Repository navigation
Expand file tree
/
Copy pathmarkdown.js
More file actions
56 lines (51 loc) · 1.4 KB
/
Copy pathmarkdown.js
File metadata and controls
56 lines (51 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
dedentToRoot,
indent,
literalline,
softline,
} from "../../document/builders.js";
import { escapeTemplateCharacters } from "../print/template-literal.js";
async function printEmbedMarkdown(textToDoc, print, path /*, options*/) {
const { node } = path;
let text = node.quasis[0].value.raw.replaceAll(
/((?:\\\\)*)\\`/gu,
(_, backslashes) => "\\".repeat(backslashes.length / 2) + "`",
);
const indentation = getIndentation(text);
const hasIndent = indentation !== "";
if (hasIndent) {
text = text.replaceAll(new RegExp(`^${indentation}`, "gmu"), "");
}
const doc = escapeTemplateCharacters(
await textToDoc(text, { parser: "markdown", __inJsTemplate: true }),
true,
);
return [
"`",
hasIndent ? indent([softline, doc]) : [literalline, dedentToRoot(doc)],
softline,
"`",
];
}
function getIndentation(str) {
const firstMatchedIndent = str.match(/^([^\S\n]*)\S/mu);
return firstMatchedIndent === null ? "" : firstMatchedIndent[1];
}
function printMarkdown(path /*, options*/) {
if (isMarkdown(path)) {
return printEmbedMarkdown;
}
}
/**
* md`...`
* markdown`...`
*/
function isMarkdown({ node, parent }) {
return (
parent?.type === "TaggedTemplateExpression" &&
node.quasis.length === 1 &&
parent.tag.type === "Identifier" &&
(parent.tag.name === "md" || parent.tag.name === "markdown")
);
}
export default printMarkdown;