fix(javascript): correct escape for markdown in js (#4381)

* test: add tests

* fix(javascript): correct escape for markdown in js

* test: add trailing newline
master
Ika 2018-04-26 18:22:43 +08:00 committed by Evilebot Tnawi
parent b46a46b112
commit ff38aaff95
3 changed files with 47 additions and 2 deletions

View File

@ -164,7 +164,10 @@ function embed(path, print, textToDoc /*, options */) {
(parentParent.tag.name === "md" || (parentParent.tag.name === "md" ||
parentParent.tag.name === "markdown"))) parentParent.tag.name === "markdown")))
) { ) {
const text = parent.quasis[0].value.cooked; const text = parent.quasis[0].value.raw.replace(
/((?:\\\\)*)\\`/g,
(_, backslashes) => "\\".repeat(backslashes.length / 2) + "`"
);
const indentation = getIndentation(text); const indentation = getIndentation(text);
const hasIndent = indentation !== ""; const hasIndent = indentation !== "";
return concat([ return concat([
@ -207,7 +210,7 @@ function escapeBackticks(doc) {
currentDoc.parts.forEach(part => { currentDoc.parts.forEach(part => {
if (typeof part === "string") { if (typeof part === "string") {
parts.push(part.replace(/`/g, "\\`")); parts.push(part.replace(/(\\*)`/g, "$1$1\\`"));
} else { } else {
parts.push(part); parts.push(part);
} }

View File

@ -69,6 +69,35 @@ markdown\\\`
`; `;
exports[`escape.js 1`] = `
markdown\`
const cssString = css\\\`
background-color: \\$\\{color('base')\\}
\\\`;
\`
markdown\`
- \\\`
- \\\\\\\`
- \\\\\\\\
- \\$
- \\u1234
\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
markdown\`
const cssString = css\\\`background-color: \\$\\{color('base')\\}\\\`;
\`;
markdown\`
* \\\`
* \\\\\\\`
* \\\\\\\\
* \\$
* \\u1234
\`;
`;
exports[`markdown.js 1`] = ` exports[`markdown.js 1`] = `
export default function ReadMe() { export default function ReadMe() {
return md\` return md\`

View File

@ -0,0 +1,13 @@
markdown`
const cssString = css\`
background-color: \$\{color('base')\}
\`;
`
markdown`
- \`
- \\\`
- \\\\
- \$
- \u1234
`