Repository navigation
Expand file tree
/
Copy pathhtml.js
More file actions
136 lines (116 loc) · 3.28 KB
/
Copy pathhtml.js
File metadata and controls
136 lines (116 loc) · 3.28 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
group,
hardline,
indent,
label,
line,
} from "../../document/builders.js";
import { mapDoc } from "../../document/utils.js";
import {
printTemplateExpressions,
uncookTemplateElementValue,
} from "../print/template-literal.js";
import { hasLanguageComment, isAngularComponentTemplate } from "./utils.js";
// The counter is needed to distinguish nested embeds.
let htmlTemplateLiteralCounter = 0;
async function printEmbedHtmlLike(parser, textToDoc, print, path, options) {
const { node } = path;
const counter = htmlTemplateLiteralCounter;
htmlTemplateLiteralCounter = (htmlTemplateLiteralCounter + 1) >>> 0;
const composePlaceholder = (index) =>
`PRETTIER_HTML_PLACEHOLDER_${index}_${counter}_IN_JS`;
const text = node.quasis
.map((quasi, index, quasis) =>
index === quasis.length - 1
? quasi.value.cooked
: quasi.value.cooked + composePlaceholder(index),
)
.join("");
const expressionDocs = printTemplateExpressions(path, print);
const placeholderRegex = new RegExp(
composePlaceholder(String.raw`(\d+)`),
"gu",
);
let topLevelCount = 0;
const doc = await textToDoc(text, {
parser,
__onHtmlRoot(root) {
topLevelCount = root.children.length;
},
});
const contentDoc = mapDoc(doc, (doc) => {
if (typeof doc !== "string") {
return doc;
}
const parts = [];
const components = doc.split(placeholderRegex);
for (let i = 0; i < components.length; i++) {
let component = components[i];
if (i % 2 === 0) {
if (component) {
component = uncookTemplateElementValue(component);
if (options.__embeddedInHtml) {
component = component.replaceAll(
/<\/(?=script\b)/giu,
String.raw`<\/`,
);
}
parts.push(component);
}
continue;
}
const placeholderIndex = Number(component);
parts.push(expressionDocs[placeholderIndex]);
}
return parts;
});
const leadingWhitespace = /^\s/u.test(text) ? " " : "";
const trailingWhitespace = /\s$/u.test(text) ? " " : "";
const linebreak =
options.htmlWhitespaceSensitivity === "ignore"
? hardline
: leadingWhitespace && trailingWhitespace
? line
: null;
if (linebreak) {
return group(["`", indent([linebreak, group(contentDoc)]), linebreak, "`"]);
}
return label(
{ hug: false },
group([
"`",
leadingWhitespace,
topLevelCount > 1 ? indent(group(contentDoc)) : group(contentDoc),
trailingWhitespace,
"`",
]),
);
}
/**
* - html`...`
* - HTML comment block
*/
function isHtml(path) {
return (
hasLanguageComment(path, "HTML") ||
path.match(
(node) => node.type === "TemplateLiteral",
(node, name) =>
node.type === "TaggedTemplateExpression" &&
node.tag.type === "Identifier" &&
node.tag.name === "html" &&
name === "quasi",
)
);
}
const printEmbedHtml = printEmbedHtmlLike.bind(undefined, "html");
const printEmbedAngular = printEmbedHtmlLike.bind(undefined, "angular");
function printHtml(path /*, options*/) {
if (isHtml(path)) {
return printEmbedHtml;
}
if (isAngularComponentTemplate(path)) {
return printEmbedAngular;
}
}
export default printHtml;