MCPcopy
hub / github.com/prettier/prettier / printEmbedHtmlLike

Function printEmbedHtmlLike

src/language-js/embed/html.js:17–103  ·  view source on GitHub ↗
(parser, textToDoc, print, path, options)

Source from the content-addressed store, hash-verified

15// The counter is needed to distinguish nested embeds.
16let htmlTemplateLiteralCounter = 0;
17async function printEmbedHtmlLike(parser, textToDoc, print, path, options) {
18 const { node } = path;
19 const counter = htmlTemplateLiteralCounter;
20 htmlTemplateLiteralCounter = (htmlTemplateLiteralCounter + 1) >>> 0;
21
22 const composePlaceholder = (index) =>
23 `PRETTIER_HTML_PLACEHOLDER_${index}_${counter}_IN_JS`;
24
25 const text = node.quasis
26 .map((quasi, index, quasis) =>
27 index === quasis.length - 1
28 ? quasi.value.cooked
29 : quasi.value.cooked + composePlaceholder(index),
30 )
31 .join("");
32
33 const expressionDocs = printTemplateExpressions(path, options, print);
34
35 const placeholderRegex = new RegExp(
36 composePlaceholder(String.raw`(\d+)`),
37 "g",
38 );
39 let topLevelCount = 0;
40 const doc = await textToDoc(text, {
41 parser,
42 __onHtmlRoot(root) {
43 topLevelCount = root.children.length;
44 },
45 });
46
47 const contentDoc = mapDoc(doc, (doc) => {
48 if (typeof doc !== "string") {
49 return doc;
50 }
51
52 const parts = [];
53
54 const components = doc.split(placeholderRegex);
55 for (let i = 0; i < components.length; i++) {
56 let component = components[i];
57
58 if (i % 2 === 0) {
59 if (component) {
60 component = uncookTemplateElementValue(component);
61 if (options.__embeddedInHtml) {
62 component = component.replaceAll(
63 /<\/(?=script\b)/gi,
64 String.raw`<\/`,
65 );
66 }
67 parts.push(component);
68 }
69 continue;
70 }
71
72 const placeholderIndex = Number(component);
73 parts.push(expressionDocs[placeholderIndex]);
74 }

Callers

nothing calls this directly

Calls 11

printTemplateExpressionsFunction · 0.90
mapDocFunction · 0.90
groupFunction · 0.90
indentFunction · 0.90
labelFunction · 0.90
composePlaceholderFunction · 0.85
textToDocFunction · 0.85
splitMethod · 0.80
mapMethod · 0.45
testMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…