From a8f69ca279539903fef4ca2095f896c118b5ba70 Mon Sep 17 00:00:00 2001 From: Wilson Hobbs Date: Fri, 10 Nov 2023 01:22:22 -0800 Subject: [PATCH] update marked --- package.json | 2 +- src/Slugger.js | 55 +++++++++++++++++++++++++++++++++++++++ src/SvelteMarkdown.svelte | 7 ++--- src/markdown-parser.js | 3 ++- yarn.lock | 8 +++--- 5 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 src/Slugger.js diff --git a/package.json b/package.json index fda9d18f..97ba4616 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ }, "dependencies": { "@types/marked": "^5.0.1", - "marked": "^5.1.2" + "marked": "^9.1.6" }, "peerDependencies": { "svelte": "^4.0.0" diff --git a/src/Slugger.js b/src/Slugger.js new file mode 100644 index 00000000..7249cc20 --- /dev/null +++ b/src/Slugger.js @@ -0,0 +1,55 @@ +/** + * Slugger generates header id + */ +export class Slugger { + constructor() { + this.seen = {}; + } + + /** + * @param {string} value + */ + serialize(value) { + return value + .toLowerCase() + .trim() + // remove html tags + .replace(/<[!\/a-z].*?>/ig, '') + // remove unwanted chars + .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '') + .replace(/\s/g, '-'); + } + + /** + * Finds the next safe (unique) slug to use + * @param {string} originalSlug + * @param {boolean} isDryRun + */ + getNextSafeSlug(originalSlug, isDryRun) { + let slug = originalSlug; + let occurenceAccumulator = 0; + if (this.seen.hasOwnProperty(slug)) { + occurenceAccumulator = this.seen[originalSlug]; + do { + occurenceAccumulator++; + slug = originalSlug + '-' + occurenceAccumulator; + } while (this.seen.hasOwnProperty(slug)); + } + if (!isDryRun) { + this.seen[originalSlug] = occurenceAccumulator; + this.seen[slug] = 0; + } + return slug; + } + + /** + * Convert string to unique id + * @param {object} [options] + * @param {boolean} [options.dryrun] Generates the next unique slug without + * updating the internal accumulator. + */ + slug(value, options = {}) { + const slug = this.serialize(value); + return this.getNextSafeSlug(slug, options.dryrun); + } + } \ No newline at end of file diff --git a/src/SvelteMarkdown.svelte b/src/SvelteMarkdown.svelte index 845f6907..ccea9921 100644 --- a/src/SvelteMarkdown.svelte +++ b/src/SvelteMarkdown.svelte @@ -1,8 +1,8 @@