Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const Markdoc = require('@markdoc/markdoc');

const DEFAULT_SCHEMA_PATH = './markdoc';

function normalize(s) {
return s.replace(/\\/g, path.win32.sep.repeat(2));
}

function getRelativeImportPath(from, to) {
const relative = path.relative(path.dirname(from), to);
if (!relative) {
return './';
}

const request = relative.startsWith('.') ? relative : `./${relative}`;
return normalize(request);
// Module specifiers always use forward slashes, on every platform.
// Emitting Windows separators (or escaping them, as the old `normalize`
// helper did for absolute paths) produces per-OS-different loader output —
// the Windows snapshot failure that got #67 reverted — and separators that
// bundlers do not treat as path delimiters.
const request = relative.split(path.sep).join(path.posix.sep);
return request.startsWith('.') ? request : `./${request}`;
}

async function gatherPartials(ast, schemaDir, tokenizer, parseOptions) {
Expand Down Expand Up @@ -85,7 +86,13 @@ async function load(source) {
// This array access @ index 1 is safe since Next.js guarantees that
// all pages will be located under either {app,pages}/ or src/{app,pages}/
// https://nextjs.org/docs/app/building-your-application/configuring/src-directory
const filepath = this.resourcePath.split(appDir ? 'app' : 'pages')[1];
// Normalized to posix separators so the emitted `path` value is identical
// on every platform (Windows resourcePaths contain backslashes). Undefined
// for resources outside {app,pages}/, e.g. .md files imported as components.
const rawFilepath = this.resourcePath.split(appDir ? 'app' : 'pages')[1];
const filepath = rawFilepath
? rawFilepath.split(path.sep).join(path.posix.sep)
: rawFilepath;

const partials = await gatherPartials.call(
this,
Expand Down
8 changes: 6 additions & 2 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,12 @@ test('mode="server"', async () => {

test('import as frontend component', async () => {
const o = options();
// Use a non-page pathway
o.resourcePath = o.resourcePath.replace('pages/test/index.md', 'components/table.md');
// Use a non-page pathway (join with OS-native separators so the replace
// also matches the backslash resourcePath on Windows)
o.resourcePath = o.resourcePath.replace(
path.join('pages', 'test', 'index.md'),
path.join('components', 'table.md')
);
const output = await callLoader(o, source);

expect(normalizeOperatingSystemPaths(output)).toMatchSnapshot();
Expand Down