Skip to content

Commit

Permalink
fix: handle missing ./
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Apr 3, 2024
1 parent 0bfd257 commit 6f69a25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions packages/integrations/markdoc/src/content-entry-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,23 @@ async function resolvePartials({
let partialContents: string;
try {
const resolved = await pluginContext.resolve(file, fileURLToPath(fileUrl));
const partialId = resolved?.id;
if (!partialId) throw new Error();
let partialId = resolved?.id;
if (!partialId) {
const attemptResolveAsRelative = await pluginContext.resolve(
'./' + file,
fileURLToPath(fileUrl)
);
if (!attemptResolveAsRelative?.id) throw new Error();
partialId = attemptResolveAsRelative.id;
}

partialPath = fileURLToPath(new URL(prependForwardSlash(partialId), 'file://'));
partialContents = await fs.promises.readFile(partialPath, 'utf-8');
} catch {
const isFormattedPath =
file.startsWith('./') || file.startsWith('../') || file.startsWith('/');
throw new MarkdocError({
message: [
`**${String(relativePartialPath)}** contains invalid content:`,
`Could not read partial file \`${file}\`. Does the file exist?${!isFormattedPath ? ' Ensure relative paths start with a `./`' : ''}`,
`Could not read partial file \`${file}\`. Does the file exist?`,
].join('\n'),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
title: Post with partials
---

{% partial file="./_partial.mdoc" /%}
{% partial file="_partial.mdoc" /%}

{% partial file="configured" /%}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This uses a custom marquee component with a shortcode:
I'm a marquee too!
{% /marquee-element %}

{% partial file="./_counter.mdoc" /%}
{% partial file="_counter.mdoc" /%}

And a code component for code blocks:

Expand Down

0 comments on commit 6f69a25

Please sign in to comment.