From a188205a259d782d7113e95f002c972f55f67472 Mon Sep 17 00:00:00 2001 From: David Worms Date: Fri, 6 Oct 2023 14:36:02 +0200 Subject: [PATCH] fix(mdx): mix slug overloading with lang --- packages/redac/lib/plugin-mdx/5.overload.js | 4 +-- packages/redac/test/plugin-mdx/5.overload.js | 30 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/redac/lib/plugin-mdx/5.overload.js b/packages/redac/lib/plugin-mdx/5.overload.js index 8339a2e..edc5866 100644 --- a/packages/redac/lib/plugin-mdx/5.overload.js +++ b/packages/redac/lib/plugin-mdx/5.overload.js @@ -4,13 +4,13 @@ export default function pluginMdxOverload (plugin) { // Index absolute slug found in filenames with metadata slug const slugs = {} documents.forEach( document => { - slugs[document.slug.join('/')] = document.data?.slug + slugs[(document.lang ?? '')+'/'+document.slug.join('/')] = document.data?.slug }) // Reconstruct the slug with metadata slug if present plugin.documents = documents.map((document) => { const newSlug = [] for (let i = 0; i < document.slug.length; i++) { - const overloadedSlug = slugs[document.slug.slice(0, i+1).join('/')] + const overloadedSlug = slugs[(document.lang ?? '')+'/'+document.slug.slice(0, i+1).join('/')] if(overloadedSlug){ newSlug[i] = overloadedSlug } else { diff --git a/packages/redac/test/plugin-mdx/5.overload.js b/packages/redac/test/plugin-mdx/5.overload.js index 70fbb9b..8d5646a 100644 --- a/packages/redac/test/plugin-mdx/5.overload.js +++ b/packages/redac/test/plugin-mdx/5.overload.js @@ -45,4 +45,34 @@ describe('mdx.overload', async () => { ]) ) }) + it('slug with lang', async () => { + await mklayout(tmpdir, [ + ['./blog/article_1.en.md', '---\nslug: my-article-1\n---'], + ['./blog/article_1.fr.md', '---\nslug: mon-article-1\n---'], + ['./blog/path-2/index.en.md', '---\nslug: my-path-3\n---'], + ['./blog/path-2/index.fr.md', '---\nslug: mon-chemin-3\n---'], + ['./blog/path-2/article_2.en.md', '---\nslug: my-article-3\n---'], + ['./blog/path-2/article_2.fr.md', '---\nslug: mon-article-3\n---'], + ]) + .then(() => + normalize({ + config: { target: `${tmpdir}/blog` }, + }) + ) + .then((plugin) => load(plugin)) + .then((plugin) => enrich(plugin)) + .then((plugin) => parse(plugin)) + .then((plugin) => overload(plugin)) + .then( + ({ documents }) => + documents.should.match([ + { lang: 'en', slug: ['my-article-1'] }, + { lang: 'fr', slug: ['mon-article-1'] }, + { lang: 'en', slug: ['my-path-3', 'my-article-3'] }, + { lang: 'fr', slug: ['mon-chemin-3', 'mon-article-3'] }, + { lang: 'en', slug: ['my-path-3'] }, + { lang: 'fr', slug: ['mon-chemin-3'] }, + ]) + ) + }) })