Skip to content
Merged
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
18 changes: 11 additions & 7 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ 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 must use forward slashes on all platforms.
// Backslashes (or escaped versions) cause different loader output per OS
// and are not treated as path delimiters by bundlers.
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 +84,12 @@ 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];
// Normalize to posix separators for consistent output across platforms.
// Undefined for non-page resources (e.g., .md 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