Skip to content

Commit

Permalink
feat(gatsby-remark-autolink-headers): add removeAccents option (#11575)
Browse files Browse the repository at this point in the history

Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
windkomo and wardpeet committed Feb 27, 2019
1 parent d7228a6 commit b6b3045
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby-remark-autolink-headers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Note: if you are using `gatsby-remark-prismjs`, make sure that it’s listed aft
- `icon`: SVG shape inside a template literal or boolean `false`. Set your own svg or disable icon (optional)
- `className`: String. Set your own class for the anchor (optional)
- `maintainCase`: Boolean. Maintains the case for markdown header (optional)
- `removeAccents`: Boolean. Remove accents from generated headings IDs (optional)

```javascript
// In your gatsby-config.js
Expand All @@ -72,6 +73,7 @@ module.exports = {
icon: `<svg aria-hidden="true" height="20" version="1.1" viewBox="0 0 16 16" width="20"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>`,
className: `custom-class`,
maintainCase: true,
removeAccents: true,
},
},
],
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-remark-autolink-headers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"github-slugger": "^1.1.1",
"lodash": "^4.17.11",
"mdast-util-to-string": "^1.0.2",
"unist-util-visit": "^1.3.0"
},
Expand Down
36 changes: 36 additions & 0 deletions packages/gatsby-remark-autolink-headers/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,40 @@ describe(`gatsby-remark-autolink-headers`, () => {
expect(node).toMatchSnapshot()
})
})

it(`keeps accents by default`, () => {
const markdownAST = remark.parse(`
# Héading One
## Héading Two
### Héading Three
`)
const removeAccents = false

const transformed = plugin({ markdownAST }, { removeAccents })

visit(transformed, `heading`, node => {
expect(node.data.id).toBeDefined()
expect(node.data.id).toEqual(expect.stringMatching(/^héading/))
})
})

it(`remove accents if removeAccents is passed`, () => {
const markdownAST = remark.parse(`
# Héading One
## Héading Two
### Héading Three
`)
const removeAccents = true

const transformed = plugin({ markdownAST }, { removeAccents })

visit(transformed, `heading`, node => {
expect(node.data.id).toBeDefined()
expect(node.data.id).toEqual(expect.stringMatching(/^heading/))
})
})
})
11 changes: 9 additions & 2 deletions packages/gatsby-remark-autolink-headers/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const toString = require(`mdast-util-to-string`)
const visit = require(`unist-util-visit`)
const slugs = require(`github-slugger`)()
const deburr = require(`lodash/deburr`)

function patch(context, key, value) {
if (!context[key]) {
Expand All @@ -14,12 +15,18 @@ const svgIcon = `<svg aria-hidden="true" focusable="false" height="16" version="

module.exports = (
{ markdownAST },
{ icon = svgIcon, className = `anchor`, maintainCase = false }
{
icon = svgIcon,
className = `anchor`,
maintainCase = false,
removeAccents = false,
}
) => {
slugs.reset()

visit(markdownAST, `heading`, node => {
const id = slugs.slug(toString(node), maintainCase)
const slug = slugs.slug(toString(node), maintainCase)
const id = removeAccents ? deburr(slug) : slug
const data = patch(node, `data`, {})

patch(data, `id`, id)
Expand Down

0 comments on commit b6b3045

Please sign in to comment.