Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made backslash escape starting delimiters. #3775

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion contrib/auto-render/splitAtDelimiters.js
Expand Up @@ -46,15 +46,24 @@ const splitAtDelimiters = function(text, delimiters) {
if (index === -1) {
break;
}

const escaped = index > 0 && text[index - 1] === "\\";
if (index > 0) {
data.push({
type: "text",
data: text.slice(0, index),
data: text.slice(0, index - (escaped ? 1 : 0)),
});
text = text.slice(index); // now text starts with delimiter
}
// ... so this always succeeds:
const i = delimiters.findIndex((delim) => text.startsWith(delim.left));

if (escaped) {
data[data.length - 1].data += text.slice(0, delimiters[i].left.length);
text = text.slice(delimiters[i].left.length);
continue;
}

index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length);
if (index === -1) {
break;
Expand Down
18 changes: 18 additions & 0 deletions contrib/auto-render/test/auto-render-spec.js
Expand Up @@ -305,6 +305,24 @@ describe("A delimiter splitter", function() {
rawData: "$$boo$$", display: true},
]);
});

it("correctly escapes start symbols", function() {
expect(splitAtDelimiters("test \\$40 for $foobar$$$bla$$\\$$boo",
[
{left:"$$", right:"$$", display:true},
{left:"$", right:"$", display:false},
])).toEqual(
[
{type: "text", data: "test $"},
{type: "text", data: "40 for "},
{type: "math", data: "foobar",
rawData: "$foobar$", display: false},
{type: "math", data: "bla",
rawData: "$$bla$$", display: true},
{type: "text", data: "$$"},
{type: "text", data: "boo"},
]);
});
});

describe("Pre-process callback", function() {
Expand Down
6 changes: 6 additions & 0 deletions docs/autorender.md
Expand Up @@ -115,6 +115,12 @@ in addition to five auto-render-specific keys:
]
```

Note that if a left delimiter is preceded by a backslash (`\`), the backslash
is stripped and the delimiter is ignored (math mode is not entered). So `\$40
is greater than \$30` gets transformed to `$40 is greater than $30`, if `$` is
configured to be a left delimiter. If it is not, the backslashes are left
untouched.

- `ignoredTags`: This is a list of DOM node types to ignore when recursing
through. The default value is
`["script", "noscript", "style", "textarea", "pre", "code", "option"]`.
Expand Down