From 475d6924b33b162003c79815a2cae024ec06959d Mon Sep 17 00:00:00 2001 From: about-code Date: Wed, 25 Sep 2019 15:42:37 +0200 Subject: [PATCH] fix: dangling anchor links with german umlauts (#27). See updates to doc/vuepress.md and slugify option. --- bin/index.js | 0 lib/glossarify.js | 21 +++++++++++++++++++++ package.json | 1 + 3 files changed, 22 insertions(+) mode change 100644 => 100755 bin/index.js diff --git a/bin/index.js b/bin/index.js old mode 100644 new mode 100755 diff --git a/lib/glossarify.js b/lib/glossarify.js index 03f594e2..dd17ac96 100644 --- a/lib/glossarify.js +++ b/lib/glossarify.js @@ -6,6 +6,7 @@ const terminator = require("./terminator"); const linker = require("./linker"); const writer = require("./writer"); const Dictionary = require("./dictionary"); +const GitHubSlugger = require("github-slugger"); const api = {}; const CWD = proc.cwd(); @@ -31,6 +32,26 @@ api.glossarify = function(opts) { .catch(err => console.error(err) && proc.exit(1)); } +/** + * Provide internally used slugifier to allow for better integration with vuepress + * See also https://github.com/about-code/glossarify-md/issues/27. + */ +api.getSlugger = function() { + return (url) => { + const slugger = new GitHubSlugger(); + return slugger.slug(url); + }; + // Implementation note: + // GitHubSlugger is stateful to be able to create unique names if the same + // anchor/headline/term occurs twice on a *single page*. But slugify function + // provided to vuepress will be invoked for different pages of a project. + // They would appear to GitHubSlugger as being a single page / namespace if + // we didn't create a new GitHubSlugger with every call, here. Rather than + // creating a new instance we could also close over a single instance and + // call 'slugger.reset()'. But, we decided to create an instance in the + // function body which can be garbage collected immediately after the call. +} + /** * @private * @param {} context diff --git a/package.json b/package.json index 3ab1b1cd..2503ab30 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "license": "MIT", "dependencies": { "fs-extra": "^8.1.0", + "github-slugger": "^1.0.0", "glob": "^7.0.3", "minimist": "^1.2.0", "minimist-options": "^4.0.1",