Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: add pageTitleFormatter config#2695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d6a463dd50e476836488a4476805File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -275,6 +275,24 @@ window.$docsify = { | ||
| }; | ||
| ``` | ||
| ## pageTitleFormatter | ||
| - Type: `Function` | ||
| - Default: `null` | ||
| Optional function to customize how the site `name` is used when composing the document title. If provided, Docsify will call this function with the configured `name` (which may contain HTML) and use the returned string as the title portion for the site name — Docsify will not automatically strip HTML or otherwise modify the value. If not provided, Docsify falls back to the default behavior of stripping HTML tags from `name`. | ||
| Basic example — strip HTML and trim (equivalent to Docsify's default behavior): | ||
sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ```js | ||
| window.$docsify = { | ||
| name: '<span>My Site</span>', | ||
| pageTitleFormatter(name) { | ||
| return name ? name.replace(/<[^>]+>/g, '').trim() : ''; | ||
| }, | ||
| }; | ||
| ``` | ||
| ## hideSidebar | ||
| - Type : `Boolean` | ||
| @@ -456,7 +474,7 @@ window.$docsify = { | ||
| ## name | ||
| - Type: `Boolean | String` | ||
| - Type: `Boolean|String` | ||
| Website name as it appears in the sidebar. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -27,6 +27,7 @@ const defaultDocsifyConfig = () => ({ | ||
| fallbackLanguages: /** @type {null | string[]} */ (null), | ||
| fallbackDefaultLanguage: '', | ||
| formatUpdated: /** @type {string | ((updatedAt: string) => string)} */ (''), | ||
| pageTitleFormatter: /** @type {null | ((name: string) => string)} */ (null), | ||
sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** For the frontmatter plugin. */ | ||
| frontMatter: /** @type {Record<string, TODO> | null} */ (null), | ||
| hideSidebar: false, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -323,13 +323,21 @@ export function Events(Base) { | ||
| * @void | ||
| */ | ||
| onRender() { | ||
| const { name } = this.config; | ||
| const { name, pageTitleFormatter } = this.config; | ||
| const currentPath = this.router.toURL(this.router.getCurrentPath()); | ||
| const currentSection = dom | ||
| .find(`.sidebar a[href='${currentPath}']`) | ||
| ?.getAttribute('title'); | ||
| const plainName = name ? name.replace(/<[^>]+>/g, '').trim() : name; | ||
| // If a pageTitleFormatter is provided, let the user format the name | ||
| // (no automatic HTML stripping). Otherwise, default to stripping | ||
| // HTML tags from the configured name. | ||
| const plainName = | ||
| typeof pageTitleFormatter === 'function' && typeof name === 'string' | ||
| ? pageTitleFormatter(name) | ||
| : name | ||
| ? name.replace(/<[^>]+>/g, '').trim() | ||
| : name; | ||
sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. sy-records marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const currentTitle = plainName | ||
| ? currentSection | ||
| ? `${currentSection} - ${plainName}` | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.