Skip to content

Commit f3ab502

Browse files
authored
feat: add pageTitleFormatter config (#2695)
1 parent 17d5fd2 commit f3ab502

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

docs/configuration.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ window.$docsify = {
275275
};
276276
```
277277

278+
## pageTitleFormatter
279+
280+
- Type: `Function`
281+
- Default: `null`
282+
283+
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`.
284+
285+
Basic example — strip HTML and trim (equivalent to Docsify's default behavior):
286+
287+
```js
288+
window.$docsify = {
289+
name: '<span>My Site</span>',
290+
pageTitleFormatter(name) {
291+
return name ? name.replace(/<[^>]+>/g, '').trim() : '';
292+
},
293+
};
294+
```
295+
278296
## hideSidebar
279297

280298
- Type : `Boolean`
@@ -456,7 +474,7 @@ window.$docsify = {
456474

457475
## name
458476

459-
- Type: `Boolean | String`
477+
- Type: `Boolean|String`
460478

461479
Website name as it appears in the sidebar.
462480

src/core/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const defaultDocsifyConfig = () => ({
2727
fallbackLanguages: /** @type {null | string[]} */ (null),
2828
fallbackDefaultLanguage: '',
2929
formatUpdated: /** @type {string | ((updatedAt: string) => string)} */ (''),
30+
pageTitleFormatter: /** @type {null | ((name: string) => string)} */ (null),
3031
/** For the frontmatter plugin. */
3132
frontMatter: /** @type {Record<string, TODO> | null} */ (null),
3233
hideSidebar: false,

src/core/event/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,21 @@ export function Events(Base) {
323323
* @void
324324
*/
325325
onRender() {
326-
const { name } = this.config;
326+
const { name, pageTitleFormatter } = this.config;
327327
const currentPath = this.router.toURL(this.router.getCurrentPath());
328328
const currentSection = dom
329329
.find(`.sidebar a[href='${currentPath}']`)
330330
?.getAttribute('title');
331331

332-
const plainName = name ? name.replace(/<[^>]+>/g, '').trim() : name;
332+
// If a pageTitleFormatter is provided, let the user format the name
333+
// (no automatic HTML stripping). Otherwise, default to stripping
334+
// HTML tags from the configured name.
335+
const plainName =
336+
typeof pageTitleFormatter === 'function' && typeof name === 'string'
337+
? pageTitleFormatter(name)
338+
: name
339+
? name.replace(/<[^>]+>/g, '').trim()
340+
: name;
333341
const currentTitle = plainName
334342
? currentSection
335343
? `${currentSection} - ${plainName}`

0 commit comments

Comments
 (0)