From 6f69a252e82f1aee8b679acee83b35c74841e962 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 3 Apr 2024 16:35:05 -0400 Subject: [PATCH] fix: handle missing ./ --- .../markdoc/src/content-entry-type.ts | 15 ++++++++++----- .../src/content/blog/with-partials.mdoc | 2 +- .../src/content/blog/with-components.mdoc | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/integrations/markdoc/src/content-entry-type.ts b/packages/integrations/markdoc/src/content-entry-type.ts index 06c76fec4964..5168c49c16a5 100644 --- a/packages/integrations/markdoc/src/content-entry-type.ts +++ b/packages/integrations/markdoc/src/content-entry-type.ts @@ -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'), }); } diff --git a/packages/integrations/markdoc/test/fixtures/render-partials/src/content/blog/with-partials.mdoc b/packages/integrations/markdoc/test/fixtures/render-partials/src/content/blog/with-partials.mdoc index 8421ca69cfbc..2d9a871101d3 100644 --- a/packages/integrations/markdoc/test/fixtures/render-partials/src/content/blog/with-partials.mdoc +++ b/packages/integrations/markdoc/test/fixtures/render-partials/src/content/blog/with-partials.mdoc @@ -2,6 +2,6 @@ title: Post with partials --- -{% partial file="./_partial.mdoc" /%} +{% partial file="_partial.mdoc" /%} {% partial file="configured" /%} diff --git a/packages/integrations/markdoc/test/fixtures/render-with-components/src/content/blog/with-components.mdoc b/packages/integrations/markdoc/test/fixtures/render-with-components/src/content/blog/with-components.mdoc index a55cf99e5e46..eb7d20426e59 100644 --- a/packages/integrations/markdoc/test/fixtures/render-with-components/src/content/blog/with-components.mdoc +++ b/packages/integrations/markdoc/test/fixtures/render-with-components/src/content/blog/with-components.mdoc @@ -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: