diff --git a/.changeset/cold-bears-sneeze.md b/.changeset/cold-bears-sneeze.md new file mode 100644 index 000000000000..483d87cd38e3 --- /dev/null +++ b/.changeset/cold-bears-sneeze.md @@ -0,0 +1,5 @@ +--- +'@astrojs/markdown-remark': minor +--- + +fixed generated slugs in markdown that ends with a dash diff --git a/packages/markdown/remark/src/rehype-collect-headers.ts b/packages/markdown/remark/src/rehype-collect-headers.ts index f8cedd0d54c0..9b9a43730156 100644 --- a/packages/markdown/remark/src/rehype-collect-headers.ts +++ b/packages/markdown/remark/src/rehype-collect-headers.ts @@ -53,7 +53,11 @@ export default function createCollectHeaders() { node as any ).value = `<${node.tagName} id={${node.properties.id}}>${raw}${node.tagName}>`; } else { - node.properties.id = slugger.slug(text); + let slug = slugger.slug(text); + + if (slug.endsWith('-')) slug = slug.slice(0, -1); + + node.properties.id = slug; } } diff --git a/packages/markdown/remark/test/expressions.test.js b/packages/markdown/remark/test/expressions.test.js index db351f9d80b1..828f70561f55 100644 --- a/packages/markdown/remark/test/expressions.test.js +++ b/packages/markdown/remark/test/expressions.test.js @@ -54,7 +54,7 @@ describe('expressions', () => { chai .expect(code) .to.equal( - '
{ foo }
is a shorthand for { foo: foo }
{ foo }
is a shorthand for { foo: foo }