(parser, textToDoc, print, path, options)
| 15 | // The counter is needed to distinguish nested embeds. |
| 16 | let htmlTemplateLiteralCounter = 0; |
| 17 | async 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…