Skip to content

Commit

Permalink
fix: special case printing json scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 24, 2024
1 parent 29cec40 commit 3c9020a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
10 changes: 6 additions & 4 deletions src/__tests__/__snapshots__/script-with-type.expected/auto.marko
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}]
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
</script>
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
script nonce=$global.cspNonce type="speculationrules"
--
{
"prerender": [{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}]
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
--
10 changes: 6 additions & 4 deletions src/__tests__/__snapshots__/script-with-type.expected/html.marko
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}]
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
</script>
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script nonce=$global.cspNonce type="speculationrules">
{
"prerender": [{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}]
"prerender": [
{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}
]
}
</script>
35 changes: 30 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ export const printers: Record<string, Printer<types.Node>> = {
return async (toDoc, print) => {
const placeholders = [] as Doc[];
const groupId = Symbol();
const parser = getScriptParser(node);
const doc: Doc[] = [
opts.markoSyntax === "html" ? "<" : "",
"script",
Expand Down Expand Up @@ -734,11 +735,13 @@ export const printers: Record<string, Printer<types.Node>> = {
: b.ifBreak("--", " --", { groupId }),
opts.markoSyntax === "html" ? "" : b.line,
replaceEmbeddedPlaceholders(
await toDoc(embeddedCode, {
parser: scriptParser,
}).catch(() =>
asLiteralTextContent(embeddedCode.trim()),
),
parser === false
? asLiteralTextContent(embeddedCode.trim())
: await toDoc(embeddedCode, {
parser,
}).catch(() =>
asLiteralTextContent(embeddedCode.trim()),
),
placeholders,
),
opts.markoSyntax === "html"
Expand Down Expand Up @@ -1258,3 +1261,25 @@ function setConfig(config: Config) {
},
};
}

function getScriptParser(tag: types.MarkoTag) {
for (const attr of tag.attributes) {
if (attr.type === "MarkoAttribute" && attr.name === "type") {
switch (
attr.value.type === "StringLiteral" ? attr.value.value : undefined
) {
case "module":
case "text/javascript":
case "application/javascript":
return scriptParser;
case "importmap":
case "speculationrules":
return "json";
default:
return false;
}
}
}

return scriptParser;
}

0 comments on commit 3c9020a

Please sign in to comment.