Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ window.$docsify = {
};
```

## pageTitleFormatter

- Type: `Function | null`
- Default: `null`

Comment thread
sy-records marked this conversation as resolved.
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):
Comment thread
sy-records marked this conversation as resolved.

```js
window.$docsify = {
name: '<span>My Site</span>',
pageTitleFormatter(name) {
return name ? name.replace(/<[^>]+>/g, '').trim() : '';
},
};
```

## hideSidebar

- Type : `Boolean`
Expand Down
1 change: 1 addition & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Comment thread
sy-records marked this conversation as resolved.
/** For the frontmatter plugin. */
frontMatter: /** @type {Record<string, TODO> | null} */ (null),
hideSidebar: false,
Expand Down
12 changes: 10 additions & 2 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
sy-records marked this conversation as resolved.
Comment thread
sy-records marked this conversation as resolved.
Comment thread
sy-records marked this conversation as resolved.
Comment thread
sy-records marked this conversation as resolved.
Comment thread
sy-records marked this conversation as resolved.
const currentTitle = plainName
? currentSection
? `${currentSection} - ${plainName}`
Expand Down
Loading