Skip to content

Commit 2aae6cc

Browse files
authored
always resolve schemaPath from next.js's root dir (#12)
Closes #118
1 parent da5505b commit 2aae6cc

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const withMarkdoc =
1010
options.defaultLoaders.babel,
1111
{
1212
loader: require.resolve('./loader'),
13-
options: pluginOptions,
13+
options: {
14+
...pluginOptions,
15+
dir: options.dir,
16+
},
1417
},
1518
],
1619
});

src/loader.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ async function load(source) {
4848
preferRelative: true,
4949
});
5050

51-
const {mode = 'static', schemaPath = DEFAULT_SCHEMA_PATH} =
52-
this.getOptions() || {};
51+
const {
52+
dir, // Root directory from Next.js (contains next.config.js)
53+
mode = 'static',
54+
schemaPath = DEFAULT_SCHEMA_PATH,
55+
} = this.getOptions() || {};
5356

54-
const schemaDir = path.resolve(schemaPath || DEFAULT_SCHEMA_PATH);
57+
const schemaDir = path.resolve(dir, schemaPath || DEFAULT_SCHEMA_PATH);
5558

5659
// Grabs the path of the file relative to the `/pages` directory
5760
// to pass into the app props later.

tests/index.test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ function options(config = {}) {
6262
const webpackThis = {
6363
context: __dirname,
6464
getOptions() {
65-
return config;
65+
return {
66+
...config,
67+
dir: __dirname,
68+
};
6669
},
6770
getLogger() {
6871
return console;
@@ -155,11 +158,11 @@ test('file output is correct', async () => {
155158

156159
test.each([
157160
[undefined, undefined],
158-
['./tests/schemas/folders', 'markdoc1'],
159-
['./tests/schemas/folders/', 'markdoc1'],
160-
['./tests/schemas/files', 'markdoc2'],
161-
['tests/schemas/files', 'markdoc2'],
162-
['tests/schemas/typescript', source],
161+
['./schemas/folders', 'markdoc1'],
162+
['./schemas/folders/', 'markdoc1'],
163+
['./schemas/files', 'markdoc2'],
164+
['schemas/files', 'markdoc2'],
165+
['schemas/typescript', source],
163166
])('Custom schema path ("%s")', async (schemaPath, expectedChild) => {
164167
const output = await callLoader(options({schemaPath}), source);
165168

@@ -174,7 +177,7 @@ test.each([
174177

175178
test('Partials', async () => {
176179
const output = await callLoader(
177-
options({schemaPath: './tests/schemas/partials'}),
180+
options({schemaPath: './schemas/partials'}),
178181
`${source}\n{% partial file="footer.md" /%}`
179182
);
180183

@@ -186,7 +189,7 @@ test('Partials', async () => {
186189

187190
test('Ejected config', async () => {
188191
const output = await callLoader(
189-
options({schemaPath: './tests/schemas/ejectedConfig'}),
192+
options({schemaPath: './schemas/ejectedConfig'}),
190193
`${source}\n{% $product %}`
191194
);
192195

0 commit comments

Comments
 (0)