From 8530cce14f9953e0e5d6b12807720e62b9b5cf1f Mon Sep 17 00:00:00 2001 From: Rafid Muhymin Wafi Date: Sat, 16 Jul 2022 15:45:41 +0600 Subject: [PATCH] Removes trailing dash from generated slugs in markdown (#3044) * fixed header slugs in markdown if ends with a dash * added changeset * removes trailing dash only if slug was created * updated test * updated change level from patch to minor --- .changeset/cold-bears-sneeze.md | 5 +++++ packages/markdown/remark/src/rehype-collect-headers.ts | 6 +++++- packages/markdown/remark/test/expressions.test.js | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .changeset/cold-bears-sneeze.md diff --git a/.changeset/cold-bears-sneeze.md b/.changeset/cold-bears-sneeze.md new file mode 100644 index 000000000..483d87cd3 --- /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 f8cedd0d5..9b9a43730 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}`; } 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 db351f9d8..828f70561 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 }

' ); });