Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):

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

## hideSidebar

- Type : `Boolean`
Expand Down Expand Up @@ -456,7 +474,7 @@ window.$docsify = {

## name

- Type: `Boolean | String`
- Type: `Boolean|String`

Website name as it appears in the sidebar.

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),
/** 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;
const currentTitle = plainName
? currentSection
? `${currentSection} - ${plainName}`
Expand Down
Loading