From 3c9020a45ccf2055978f515a490b77fbbcf4ad29 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Wed, 23 Oct 2024 19:04:32 -0700 Subject: [PATCH] fix: special case printing json scripts --- .../script-with-type.expected/auto.marko | 10 +++--- .../script-with-type.expected/concise.marko | 10 +++--- .../script-with-type.expected/html.marko | 10 +++--- .../with-parens.marko | 10 +++--- src/index.ts | 35 ++++++++++++++++--- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/__tests__/__snapshots__/script-with-type.expected/auto.marko b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko index 92dcfc9..dc82cd2 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/auto.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/auto.marko @@ -1,8 +1,10 @@ diff --git a/src/__tests__/__snapshots__/script-with-type.expected/concise.marko b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko index 593d4e4..077b6d7 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/concise.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/concise.marko @@ -1,9 +1,11 @@ script nonce=$global.cspNonce type="speculationrules" -- { - "prerender": [{ - "where": { "href_matches": "/*" }, - "eagerness": "moderate" - }] + "prerender": [ + { + "where": { "href_matches": "/*" }, + "eagerness": "moderate" + } + ] } -- diff --git a/src/__tests__/__snapshots__/script-with-type.expected/html.marko b/src/__tests__/__snapshots__/script-with-type.expected/html.marko index 92dcfc9..dc82cd2 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/html.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/html.marko @@ -1,8 +1,10 @@ diff --git a/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko index 92dcfc9..dc82cd2 100644 --- a/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko +++ b/src/__tests__/__snapshots__/script-with-type.expected/with-parens.marko @@ -1,8 +1,10 @@ diff --git a/src/index.ts b/src/index.ts index 1a87426..a008d01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -645,6 +645,7 @@ export const printers: Record> = { return async (toDoc, print) => { const placeholders = [] as Doc[]; const groupId = Symbol(); + const parser = getScriptParser(node); const doc: Doc[] = [ opts.markoSyntax === "html" ? "<" : "", "script", @@ -734,11 +735,13 @@ export const printers: Record> = { : 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" @@ -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; +}