Skip to content

Commit c068c08

Browse files
authored
allowComments in next.js plugin (#24)
* allowComments in next.js plugin * pass as tokenizerOptions
1 parent dacc064 commit c068c08

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/loader.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function normalize(s) {
88
return s.replace(/\\/g, path.win32.sep.repeat(2));
99
}
1010

11-
async function gatherPartials(ast, schemaDir) {
11+
async function gatherPartials(ast, schemaDir, tokenizer) {
1212
let partials = {};
1313

1414
for (const node of ast.walk()) {
@@ -25,11 +25,12 @@ async function gatherPartials(ast, schemaDir) {
2525
const content = await fs.promises.readFile(filepath, {encoding: 'utf8'});
2626

2727
if (content) {
28-
const ast = Markdoc.parse(content);
28+
const tokens = tokenizer.tokenize(content);
29+
const ast = Markdoc.parse(tokens);
2930
partials = {
3031
...partials,
3132
[file]: content,
32-
...(await gatherPartials.call(this, ast, schemaDir)),
33+
...(await gatherPartials.call(this, ast, schemaDir, tokenizer)),
3334
};
3435
}
3536
}
@@ -51,10 +52,14 @@ async function load(source) {
5152
dir, // Root directory from Next.js (contains next.config.js)
5253
mode = 'static',
5354
schemaPath = DEFAULT_SCHEMA_PATH,
55+
tokenizerOptions = undefined,
5456
} = this.getOptions() || {};
5557

58+
const tokenizer = new Markdoc.Tokenizer(tokenizerOptions);
59+
5660
const schemaDir = path.resolve(dir, schemaPath || DEFAULT_SCHEMA_PATH);
57-
const ast = Markdoc.parse(source);
61+
const tokens = tokenizer.tokenize(source);
62+
const ast = Markdoc.parse(tokens);
5863

5964
// Grabs the path of the file relative to the `/pages` directory
6065
// to pass into the app props later.
@@ -66,7 +71,8 @@ async function load(source) {
6671
const partials = await gatherPartials.call(
6772
this,
6873
ast,
69-
path.resolve(schemaDir, 'partials')
74+
path.resolve(schemaDir, 'partials'),
75+
tokenizer
7076
);
7177

7278
// IDEA: consider making this an option per-page
@@ -124,12 +130,17 @@ import {getSchema, defaultObject} from '${normalize(
124130
*/
125131
${schemaCode}
126132
133+
const tokenizer = new Markdoc.Tokenizer(${
134+
tokenizerOptions ? JSON.stringify(tokenizerOptions) : ''
135+
});
136+
127137
/**
128138
* Source will never change at runtime, so parse happens at the file root
129139
*/
130140
const source = ${JSON.stringify(source)};
131141
const filepath = ${JSON.stringify(filepath)};
132-
const ast = Markdoc.parse(source);
142+
const tokens = tokenizer.tokenize(source);
143+
const ast = Markdoc.parse(tokens);
133144
134145
/**
135146
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -146,7 +157,8 @@ export async function ${dataFetchingFunction}(context) {
146157
147158
// Ensure Node.transformChildren is available
148159
Object.keys(partials).forEach((key) => {
149-
partials[key] = Markdoc.parse(partials[key]);
160+
const tokens = tokenizer.tokenize(partials[key]);
161+
partials[key] = Markdoc.parse(tokens);
150162
});
151163
152164
const cfg = {

tests/__snapshots__/index.test.js.snap

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import {getSchema, defaultObject} from './src/runtime.js';
1313
*/
1414
const schema = {};
1515
16+
const tokenizer = new Markdoc.Tokenizer();
17+
1618
/**
1719
* Source will never change at runtime, so parse happens at the file root
1820
*/
1921
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
2022
const filepath = \\"/test/index.md\\";
21-
const ast = Markdoc.parse(source);
23+
const tokens = tokenizer.tokenize(source);
24+
const ast = Markdoc.parse(tokens);
2225
2326
/**
2427
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -35,7 +38,8 @@ export async function getStaticProps(context) {
3538
3639
// Ensure Node.transformChildren is available
3740
Object.keys(partials).forEach((key) => {
38-
partials[key] = Markdoc.parse(partials[key]);
41+
const tokens = tokenizer.tokenize(partials[key]);
42+
partials[key] = Markdoc.parse(tokens);
3943
});
4044
4145
const cfg = {
@@ -99,12 +103,15 @@ import {getSchema, defaultObject} from './src/runtime.js';
99103
*/
100104
const schema = {};
101105
106+
const tokenizer = new Markdoc.Tokenizer();
107+
102108
/**
103109
* Source will never change at runtime, so parse happens at the file root
104110
*/
105111
const source = \\"---\\\\ntitle: Custom title\\\\n---\\\\n\\\\n# {% $markdoc.frontmatter.title %}\\\\n\\\\n{% tag /%}\\\\n\\";
106112
const filepath = \\"/test/index.md\\";
107-
const ast = Markdoc.parse(source);
113+
const tokens = tokenizer.tokenize(source);
114+
const ast = Markdoc.parse(tokens);
108115
109116
/**
110117
* Like the AST, frontmatter won't change at runtime, so it is loaded at file root.
@@ -121,7 +128,8 @@ export async function getStaticProps(context) {
121128
122129
// Ensure Node.transformChildren is available
123130
Object.keys(partials).forEach((key) => {
124-
partials[key] = Markdoc.parse(partials[key]);
131+
const tokens = tokenizer.tokenize(partials[key]);
132+
partials[key] = Markdoc.parse(tokens);
125133
});
126134
127135
const cfg = {

0 commit comments

Comments
 (0)