-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preprocess-auto-slug): mature implementation and customization
- Loading branch information
1 parent
6d31c89
commit cb2cd7f
Showing
10 changed files
with
245 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@svelte-put/preprocess-auto-slug': minor | ||
--- | ||
|
||
more mature options and ability to customization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/preprocessors/auto-slug/src/auto-slug.constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AutoSlugOptions } from './auto-slug.types'; | ||
|
||
/** | ||
* default options for auto-slug | ||
* | ||
* @public | ||
*/ | ||
export const DEFAULT_AUTO_SLUG_OPTIONS: AutoSlugOptions = { | ||
tags: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], | ||
attributeName: 'id', | ||
slug: ({ generated }) => generated, | ||
anchor: { | ||
enabled: true, | ||
position: 'prepend', | ||
content: '#', | ||
properties: { | ||
'aria-hidden': 'true', | ||
'tab-index': '-1', | ||
}, | ||
href: (slug) => `#${slug}`, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { BaseNode } from 'estree'; | ||
import type BananaSlug from 'github-slugger'; | ||
|
||
/** @internal */ | ||
type PartialAutoSlugOptions = Omit<AutoSlugOptions, 'anchor'> & { | ||
anchor?: Partial<AutoSlugOptions['anchor']>; | ||
}; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export type AutoSlugInput = | ||
| PartialAutoSlugOptions | ||
| ((defaultOptions: AutoSlugOptions) => PartialAutoSlugOptions); | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface AutoSlugAnchorOptions { | ||
enabled: boolean; | ||
/** | ||
* where to create the anchor tag | ||
* | ||
* - 'prepend' — inject link before the target tag text | ||
* - 'append' — inject link after the target tag text | ||
* - 'wrap' — wrap the whole target tag text with the link | ||
* - 'before' — insert link before the target tag | ||
* - 'after' — insert link after the target tag | ||
*/ | ||
position: 'prepend' | 'append' | 'wrap' | 'before' | 'after'; | ||
/** default to '#', ignored when behavior is `wrap` */ | ||
content: string; | ||
/** defaults to { 'aria-hidden': 'true', 'tab-index': '-1' } */ | ||
properties: Record<string, string>; | ||
href: (slug: string) => string; | ||
} | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface SlugResolverInput { | ||
/** generated slug, by default slug will resolve to this */ | ||
generated: string; | ||
/** text extracted from original node */ | ||
nodeText: string; | ||
/** the {@link https://github.com/Flet/github-slugger | BananaSlug} object */ | ||
slugger: BananaSlug; | ||
} | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface AutoSlugOptions { | ||
/** target tag, default to all heading tags */ | ||
tags: string[]; | ||
/** default to `id` */ | ||
attributeName: string; | ||
/** instructions to add the anchor tag */ | ||
anchor: AutoSlugAnchorOptions; | ||
/** slug resolver */ | ||
slug: (SlugResolverInput) => string; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface Node extends BaseNode { | ||
name: string; | ||
start: number; | ||
end: number; | ||
attributes: Array<{ name: string; type: string }>; | ||
children?: Array<Node>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
data?: any; | ||
} |
Oops, something went wrong.