From f4514e0ee988dcdaaca9a02b17359d5bfb80c1d5 Mon Sep 17 00:00:00 2001 From: Kaciras Date: Wed, 22 Feb 2023 20:15:36 +0800 Subject: [PATCH] pass env to `slugify` --- index.js | 2 +- test.js | 7 +++++++ types/index.d.ts | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6a850c0..a9bb6bc 100644 --- a/index.js +++ b/index.js @@ -58,7 +58,7 @@ function anchor (md, opts) { let slug = token.attrGet('id') if (slug == null) { - slug = uniqueSlug(opts.slugify(title), slugs, false, opts.uniqueSlugStartIndex) + slug = uniqueSlug(opts.slugify(title, state.env), slugs, false, opts.uniqueSlugStartIndex) } else { slug = uniqueSlug(slug, slugs, true, opts.uniqueSlugStartIndex) } diff --git a/test.js b/test.js index f7c8169..b764e4a 100644 --- a/test.js +++ b/test.js @@ -232,6 +232,13 @@ test('getTokensText', t => { ) }) +test('slugify', t => { + t.is( + md().use(anchor, { slugify: (title, env) => `${env.docId}-${title}` }).render('# bar', { docId: 'foo' }), + '

bar

\n' + ) +}) + nest('permalink.linkInsideHeader', test => { test('default', t => { t.is( diff --git a/types/index.d.ts b/types/index.d.ts index 7c94aae..d3faa03 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -47,7 +47,7 @@ declare namespace anchor { export interface AnchorOptions { level?: number | number[]; - slugify?(str: string): string; + slugify?(str: string, env: any): string; getTokensText?(tokens: Token[]): string; uniqueSlugStartIndex?: number;