Skip to content

Commit

Permalink
Merge pull request #125 from Kaciras/master
Browse files Browse the repository at this point in the history
Pass env to `slugify`
  • Loading branch information
valeriangalliat authored Aug 24, 2024
2 parents 1365db3 + f4514e0 commit 2e889a1
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 2e889a1

Please sign in to comment.