Skip to content

Commit

Permalink
Fix bug with invalid example IDs
Browse files Browse the repository at this point in the history
This reverts a hotfix #809
that removed the changes in favour of a proper fix for
#620

Generating example IDs from example titles can be brittle if there
are unexpected characters in titles. Use slugger for more robust converting of
exampleTitle to exampleId. This also uses the solution from #620 to fix
duplicate IDs by adding an extra parameter "titleSuffix" to examples if
necessary.

Co-authored-by: Nick Colley <[email protected]>
  • Loading branch information
hannalaakso and NickColley committed Mar 4, 2019
1 parent 82bfeeb commit 60d73aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/metalsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const modernizrBuild = require('./modernizr-build.js') // modernizr build plugin
const generateSitemap = require('./generate-sitemap.js') // generate sitemap
const lunr = require('./metalsmith-lunr-index') // generate search index
const extractPageHeadings = require('../lib/extract-page-headings/index.js') // extract page headings into file meta data
const slugger = require('slugger') // generate slugs from titles

const colours = require('../lib/colours.js') // get colours data
const fileHelper = require('../lib/file-helper.js') // helper function to operate on files
Expand Down Expand Up @@ -218,7 +219,7 @@ module.exports = metalsmith(__dirname) // __dirname defined by node.js: name of
},
filters: {
highlight: highlighter,
kebabCase: string => string.replace(/\s+/g, '-').toLowerCase()
slugger
},

// Markdown engine options
Expand Down
19 changes: 12 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"metalsmith-postcss": "^4.2.0",
"modernizr": "^3.6.0",
"sass-export": "^1.0.2",
"serve-static": "^1.13.2"
"serve-static": "^1.13.2",
"slugger": "^1.0.1"
},
"devDependencies": {
"accessible-autocomplete": "^1.6.2",
Expand Down
8 changes: 7 additions & 1 deletion views/partials/_example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
{% endif %}
{% set exampleTitle = exampleTitle + " example" %}

{% set exampleId = exampleTitle | kebabCase %}
{% if params.id %}
{% set exampleId = params.id %}
{% else %}
{% set exampleId = exampleTitle | slugger %}
{% endif %}

{% if params.open %}
{% set exampleId = exampleId + '-open' %}
{% endif %}

{% set display = params.displayExample | default(true) %}

{% set multipleTabs = params.html and params.nunjucks %}
Expand Down

0 comments on commit 60d73aa

Please sign in to comment.