Skip to content

Commit

Permalink
pass env to slugify
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaciras committed Feb 22, 2023
1 parent 649582d commit f4514e0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }),
'<h1 id="foo-bar" tabindex="-1">bar</h1>\n'
)
})

nest('permalink.linkInsideHeader', test => {
test('default', t => {
t.is(
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f4514e0

Please sign in to comment.