Skip to content

Commit

Permalink
Removes trailing dash from generated slugs in markdown (#3044)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RafidMuhymin authored and FredKSchott committed Jul 19, 2022
1 parent 4bb96ac commit ba11b33
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-bears-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': minor
---

fixed generated slugs in markdown that ends with a dash
6 changes: 5 additions & 1 deletion packages/markdown/remark/src/rehype-collect-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/remark/test/expressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('expressions', () => {
chai
.expect(code)
.to.equal(
'<h1 id="-foo--is-a-shorthand-for--foo-foo-"><code is:raw>{ foo }</code> is a shorthand for <code is:raw>{ foo: foo }</code></h1>'
'<h1 id="-foo--is-a-shorthand-for--foo-foo"><code is:raw>{ foo }</code> is a shorthand for <code is:raw>{ foo: foo }</code></h1>'
);
});

Expand Down

0 comments on commit ba11b33

Please sign in to comment.