Skip to content

Commit a75fb62

Browse files
committed
[hugo] fix(converter): bundle nested meshesYouLearn imagepath values
IMAGE_KEY only matches top-level front-matter keys, so the indented imagepath entries inside the learning-path meshesYouLearn list were copied through as ../../../src/... paths that don't exist in the Hugo site. Bundle them like cardImage, keeping indentation, quotes and the trailing comma, and warn when one can't be bundled. Fixes #8636 Signed-off-by: hiyach28 <hiyach28@gmail.com>
1 parent d603924 commit a75fb62

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎hugo/scripts/migrate-mdx.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,9 @@ export function gatsbyRoute(collection, rel, fm) {
10211021
const IMAGE_KEY = /^(thumbnail|darkthumbnail|thumbnail_svg|darkthumbnail_svg|image|cover|cardImage)\s*:\s*(.*)$/;
10221022
// Keys Hugo reserves for itself: `type`/`layout` would pick templates (`type: Blog` → layouts/Blog/), so they are renamed.
10231023
const FM_RENAME = { type: 'content_type', layout: 'gatsby_layout' };
1024+
// Image paths nested inside a list, e.g. the learning-path `meshesYouLearn: [{ imagepath: "...", name: ... }]`.
1025+
// Keeps the indentation, quote style and trailing comma so the flow-style YAML stays valid.
1026+
const NESTED_IMAGE_KEY = /^(\s+)(imagepath)(\s*:\s*)(['"]?)(.*?)\4(\s*,?)\s*$/;
10241027

10251028
/**
10261029
* `route` (from gatsbyRoute) adds the explicit `url:` with a trailing slash, `draft: true` for unpublished documents,
@@ -1123,6 +1126,20 @@ export function convertFrontMatter(fm, slug, ctx, route) {
11231126
continue;
11241127
}
11251128

1129+
if ((m = NESTED_IMAGE_KEY.exec(line))) {
1130+
const [, indent, key, sep, quote, v, tail] = m;
1131+
const name = /^\.\.\//.test(v) && ctx.bundle?.(v);
1132+
if (name) {
1133+
out.push(`${indent}${key}${sep}${quote}${name}${quote}${tail}`);
1134+
} else {
1135+
if (/^\.\.\//.test(v) || /^\/?(static|assets|src)\//.test(v)) {
1136+
ctx.warn(`${key}: "${v}" is outside the page bundle — move it next to index.md`);
1137+
}
1138+
out.push(line);
1139+
}
1140+
continue;
1141+
}
1142+
11261143
if (/^slug\s*:/.test(line)) hasSlug = true;
11271144
out.push(line);
11281145
}

‎hugo/scripts/test/migrate-mdx.test.mjs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,3 +2210,32 @@ test('MesheryDesignEmbed becomes {{< meshery-embed >}} with snake_case params; s
22102210
assert.deepEqual(stats.unknown, {});
22112211
assert.match(convertFile(src, { slug: 'e', file: 'sistent/e/index.mdx' }).output, /<MesheryDesignEmbed/);
22122212
});
2213+
2214+
test('nested meshesYouLearn imagepath values are bundled, and unbundlable ones warn', () => {
2215+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'l5-imagepath-'));
2216+
fs.mkdirSync(path.join(tmp, 'assets'), { recursive: true });
2217+
fs.writeFileSync(path.join(tmp, 'assets/istio.svg'), '<svg></svg>');
2218+
const dir = path.join(tmp, 'content-learn/course');
2219+
fs.mkdirSync(dir, { recursive: true });
2220+
const src = `---
2221+
title: "Course"
2222+
meshesYouLearn:
2223+
[
2224+
{
2225+
imagepath: "../../assets/istio.svg",
2226+
name: "Istio",
2227+
},
2228+
{
2229+
imagepath: '../../assets/missing.svg',
2230+
name: "Missing",
2231+
},
2232+
]
2233+
---
2234+
Hello
2235+
`;
2236+
const { output, stats } = convertFile(src, { slug: 'course', file: 'content-learn/course/index.mdx', collection: 'content-learn', dir });
2237+
assert.match(output, /^ imagepath: "istio\.svg",$/m);
2238+
assert.match(output, /^ imagepath: '\.\.\/\.\.\/assets\/missing\.svg',$/m);
2239+
assert.deepEqual(stats.assets.map((a) => a.name), ['istio.svg']);
2240+
assert.ok(stats.warnings.some((w) => w.includes('imagepath: "../../assets/missing.svg" is outside the page bundle')));
2241+
});

0 commit comments

Comments
 (0)