From 134b12d7e387c6247c718eb603907da09ff518c1 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Tue, 25 Jul 2023 10:54:35 -0700 Subject: [PATCH] support slots --- package-lock.json | 84 +++++++++++++++++++++++--- src/loader.js | 8 ++- tests/__snapshots__/index.test.js.snap | 4 +- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1a37eea..32433c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "devDependencies": { "@babel/core": "^7.16.12", "@babel/plugin-transform-modules-commonjs": "^7.16.8", - "@markdoc/markdoc": "*", + "@markdoc/markdoc": "^0.3.0", "@types/jest": "^27.4.1", "enhanced-resolve": "^5.10.0", "jest": "^27.5.1", @@ -1203,12 +1203,27 @@ } }, "node_modules/@markdoc/markdoc": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/@markdoc/markdoc/-/markdoc-0.1.7.tgz", - "integrity": "sha512-Dz7+VP5I3m/DFpLnzjwvXv64XmneYNzy4adm5uJSkcAJxFlJujIn+hCmMh11k70w+y5Qu18sjngetpuP+3570g==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@markdoc/markdoc/-/markdoc-0.3.0.tgz", + "integrity": "sha512-QWCF8krIIw52ulflfnoff0yG1eKl9CCGA3KAiOjHyYtHNzSEouFh8lO52nAaO3qV2Ctj1GTB8TTb2rTfvISQfA==", "dev": true, "engines": { "node": ">=14.7.0" + }, + "optionalDependencies": { + "@types/markdown-it": "12.2.3" + }, + "peerDependencies": { + "@types/react": "*", + "react": "*" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "react": { + "optional": true + } } }, "node_modules/@next/env": { @@ -1520,6 +1535,31 @@ "pretty-format": "^27.0.0" } }, + "node_modules/@types/linkify-it": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.2.tgz", + "integrity": "sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==", + "dev": true, + "optional": true + }, + "node_modules/@types/markdown-it": { + "version": "12.2.3", + "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-12.2.3.tgz", + "integrity": "sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==", + "dev": true, + "optional": true, + "dependencies": { + "@types/linkify-it": "*", + "@types/mdurl": "*" + } + }, + "node_modules/@types/mdurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-1.0.2.tgz", + "integrity": "sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==", + "dev": true, + "optional": true + }, "node_modules/@types/node": { "version": "17.0.31", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.31.tgz", @@ -7037,10 +7077,13 @@ } }, "@markdoc/markdoc": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/@markdoc/markdoc/-/markdoc-0.1.7.tgz", - "integrity": "sha512-Dz7+VP5I3m/DFpLnzjwvXv64XmneYNzy4adm5uJSkcAJxFlJujIn+hCmMh11k70w+y5Qu18sjngetpuP+3570g==", - "dev": true + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@markdoc/markdoc/-/markdoc-0.3.0.tgz", + "integrity": "sha512-QWCF8krIIw52ulflfnoff0yG1eKl9CCGA3KAiOjHyYtHNzSEouFh8lO52nAaO3qV2Ctj1GTB8TTb2rTfvISQfA==", + "dev": true, + "requires": { + "@types/markdown-it": "12.2.3" + } }, "@next/env": { "version": "12.1.6", @@ -7240,6 +7283,31 @@ "pretty-format": "^27.0.0" } }, + "@types/linkify-it": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.2.tgz", + "integrity": "sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==", + "dev": true, + "optional": true + }, + "@types/markdown-it": { + "version": "12.2.3", + "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-12.2.3.tgz", + "integrity": "sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==", + "dev": true, + "optional": true, + "requires": { + "@types/linkify-it": "*", + "@types/mdurl": "*" + } + }, + "@types/mdurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-1.0.2.tgz", + "integrity": "sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==", + "dev": true, + "optional": true + }, "@types/node": { "version": "17.0.31", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.31.tgz", diff --git a/src/loader.js b/src/loader.js index ed81442..288c5fc 100644 --- a/src/loader.js +++ b/src/loader.js @@ -52,14 +52,16 @@ async function load(source) { dir, // Root directory from Next.js (contains next.config.js) mode = 'static', schemaPath = DEFAULT_SCHEMA_PATH, - tokenizerOptions = {allowComments: true}, + tokenizerOptions: {slots = false, ...tokenizerOptions} = { + allowComments: true, + }, } = this.getOptions() || {}; const tokenizer = new Markdoc.Tokenizer(tokenizerOptions); const schemaDir = path.resolve(dir, schemaPath || DEFAULT_SCHEMA_PATH); const tokens = tokenizer.tokenize(source); - const ast = Markdoc.parse(tokens); + const ast = Markdoc.parse(tokens, {slots}); // Grabs the path of the file relative to the `/pages` directory // to pass into the app props later. @@ -140,7 +142,7 @@ const tokenizer = new Markdoc.Tokenizer(${ const source = ${JSON.stringify(source)}; const filepath = ${JSON.stringify(filepath)}; const tokens = tokenizer.tokenize(source); -const ast = Markdoc.parse(tokens); +const ast = Markdoc.parse(tokens, {slots: ${JSON.stringify(slots)}}); /** * Like the AST, frontmatter won't change at runtime, so it is loaded at file root. diff --git a/tests/__snapshots__/index.test.js.snap b/tests/__snapshots__/index.test.js.snap index ca1a4c9..9bd738b 100644 --- a/tests/__snapshots__/index.test.js.snap +++ b/tests/__snapshots__/index.test.js.snap @@ -21,7 +21,7 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true}); const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\"; const filepath = \\"/test/index.md\\"; const tokens = tokenizer.tokenize(source); -const ast = Markdoc.parse(tokens); +const ast = Markdoc.parse(tokens, {slots: false}); /** * Like the AST, frontmatter won't change at runtime, so it is loaded at file root. @@ -111,7 +111,7 @@ const tokenizer = new Markdoc.Tokenizer({\\"allowComments\\":true}); const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\"; const filepath = \\"/test/index.md\\"; const tokens = tokenizer.tokenize(source); -const ast = Markdoc.parse(tokens); +const ast = Markdoc.parse(tokens, {slots: false}); /** * Like the AST, frontmatter won't change at runtime, so it is loaded at file root.