Skip to content

Commit 1482663

Browse files
committed
fix: special case printing json scripts
1 parent 2066cc1 commit 1482663

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script nonce=$global.cspNonce type="speculationrules">
22
{
3-
"prerender": [{
4-
"where": { "href_matches": "/*" },
5-
"eagerness": "moderate"
6-
}]
3+
"prerender": [
4+
{
5+
"where": { "href_matches": "/*" },
6+
"eagerness": "moderate"
7+
}
8+
]
79
}
810
</script>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
script nonce=$global.cspNonce type="speculationrules"
22
--
33
{
4-
"prerender": [{
5-
"where": { "href_matches": "/*" },
6-
"eagerness": "moderate"
7-
}]
4+
"prerender": [
5+
{
6+
"where": { "href_matches": "/*" },
7+
"eagerness": "moderate"
8+
}
9+
]
810
}
911
--
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script nonce=$global.cspNonce type="speculationrules">
22
{
3-
"prerender": [{
4-
"where": { "href_matches": "/*" },
5-
"eagerness": "moderate"
6-
}]
3+
"prerender": [
4+
{
5+
"where": { "href_matches": "/*" },
6+
"eagerness": "moderate"
7+
}
8+
]
79
}
810
</script>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script nonce=$global.cspNonce type="speculationrules">
22
{
3-
"prerender": [{
4-
"where": { "href_matches": "/*" },
5-
"eagerness": "moderate"
6-
}]
3+
"prerender": [
4+
{
5+
"where": { "href_matches": "/*" },
6+
"eagerness": "moderate"
7+
}
8+
]
79
}
810
</script>

src/index.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ export const printers: Record<string, Printer<types.Node>> = {
645645
return async (toDoc, print) => {
646646
const placeholders = [] as Doc[];
647647
const groupId = Symbol();
648+
const parser = getScriptParser(node);
648649
const doc: Doc[] = [
649650
opts.markoSyntax === "html" ? "<" : "",
650651
"script",
@@ -734,11 +735,13 @@ export const printers: Record<string, Printer<types.Node>> = {
734735
: b.ifBreak("--", " --", { groupId }),
735736
opts.markoSyntax === "html" ? "" : b.line,
736737
replaceEmbeddedPlaceholders(
737-
await toDoc(embeddedCode, {
738-
parser: scriptParser,
739-
}).catch(() =>
740-
asLiteralTextContent(embeddedCode.trim()),
741-
),
738+
parser === false
739+
? asLiteralTextContent(embeddedCode.trim())
740+
: await toDoc(embeddedCode, {
741+
parser,
742+
}).catch(() =>
743+
asLiteralTextContent(embeddedCode.trim()),
744+
),
742745
placeholders,
743746
),
744747
opts.markoSyntax === "html"
@@ -1258,3 +1261,25 @@ function setConfig(config: Config) {
12581261
},
12591262
};
12601263
}
1264+
1265+
function getScriptParser(tag: types.MarkoTag) {
1266+
for (const attr of tag.attributes) {
1267+
if (attr.type === "MarkoAttribute" && attr.name === "type") {
1268+
switch (
1269+
attr.value.type === "StringLiteral" ? attr.value.value : undefined
1270+
) {
1271+
case "module":
1272+
case "text/javascript":
1273+
case "application/javascript":
1274+
return scriptParser;
1275+
case "importmap":
1276+
case "speculationrules":
1277+
return "json";
1278+
default:
1279+
return false;
1280+
}
1281+
}
1282+
}
1283+
1284+
return scriptParser;
1285+
}

0 commit comments

Comments
 (0)