Skip to content

Commit

Permalink
fix(mdx): mix slug overloading with lang
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Oct 6, 2023
1 parent c44a14b commit a188205
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/redac/lib/plugin-mdx/5.overload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions packages/redac/test/plugin-mdx/5.overload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] },
])
)
})
})

0 comments on commit a188205

Please sign in to comment.