Skip to content

Commit 7b15c6b

Browse files
author
Leonid Buneev
committed
1 parent 82fedd2 commit 7b15c6b

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ All notable changes to this project will be documented in this file.
1212
- Support for hooks functions (@bobheadxi in #10)
1313
- Ability to set search query from URL parameter (@bobheadxi in #10)
1414
- Ability to exclude page from search (@bobheadxi in #10)
15+
- Specified "simple" encoder for english-based languages to allow fuzzy search (see #22)
16+
- If page title is not specified on page itself, title will be taken from sidebar (#21)

index.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
const { path } = require('@vuepress/shared-utils')
22
const htmlToText = require('html-to-text')
3+
const _ = require('lodash')
34

4-
module.exports = options => ({
5+
let customTitles = null
6+
7+
module.exports = (options, ctx, globalCtx) => ({
58
extendPageData($page) {
69
try {
710
const { html } = $page._context.markdown.render($page._strippedContent || '')
11+
if (!customTitles) customTitles = getCustomTitles(globalCtx)
812

913
const plaintext = htmlToText.fromString(html, {
1014
wordwrap: null,
@@ -24,6 +28,9 @@ module.exports = options => ({
2428
$page.content = plaintext
2529
$page.contentLowercase = plaintext.toLowerCase()
2630
$page.charsets = getCharsets(plaintext)
31+
32+
// Take title from sidebar if it's missing on the page itself
33+
if (!$page.title) $page.title = customTitles[normalizePath($page.path)]
2734
} catch (e) {
2835
// incorrect markdown
2936
console.error('Error when applying fulltext-search plugin:', e)
@@ -40,6 +47,60 @@ module.exports = options => ({
4047
},
4148
})
4249

50+
function getCustomTitles(globalCtx) {
51+
try {
52+
const sidebarConfig = _.get(globalCtx, '_pluginContext.themeConfig.sidebar')
53+
if (!sidebarConfig) return {}
54+
55+
let sidebars = [sidebarConfig]
56+
if (_.isPlainObject(sidebarConfig)) sidebars = _.values(sidebarConfig)
57+
58+
sidebars = sidebars.filter(sb => _.isArray(sb))
59+
60+
const result = {}
61+
for (const sb of sidebars) {
62+
for (const page of sb) {
63+
if (_.isArray(page)) {
64+
const [pathWithExtension, title] = page
65+
const normalizedPath = normalizePath(pathWithExtension)
66+
if (normalizedPath && title) result[normalizedPath] = title
67+
continue
68+
}
69+
if (!_.isObjectLike(page)) continue
70+
71+
if (page.path && page.title) {
72+
const normalizedPath = normalizePath(page.path)
73+
if (normalizedPath) result[normalizedPath] = page.title
74+
}
75+
if (page.children) {
76+
for (const c of page.children) {
77+
if (_.isArray(c)) {
78+
const [pathWithExtension, title] = c
79+
const normalizedPath = normalizePath(pathWithExtension)
80+
if (normalizedPath && title) result[normalizedPath] = title
81+
}
82+
}
83+
}
84+
}
85+
}
86+
return result
87+
} catch (e) {
88+
console.log('[fulltext-search] Error while getting sidebar paths:', e)
89+
return {}
90+
}
91+
}
92+
93+
function normalizePath(rawPath) {
94+
if (!rawPath) return null
95+
try {
96+
const parsedPath = path.parse(rawPath)
97+
return path.join(parsedPath.dir, parsedPath.name)
98+
} catch (e) {
99+
console.log(`[fulltext-search] Error while normalizing path ${rawPath}:`, e)
100+
return null
101+
}
102+
}
103+
43104
function getCharsets(text) {
44105
const cyrillicRegex = /[\u0400-\u04FF]/iu
45106
const cjkRegex = /[\u3131-\u314e|\u314f-\u3163|\uac00-\ud7a3]|[\u4E00-\u9FCC\u3400-\u4DB5\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\ud840-\ud868][\udc00-\udfff]|\ud869[\udc00-\uded6\udf00-\udfff]|[\ud86a-\ud86c][\udc00-\udfff]|\ud86d[\udc00-\udf34\udf40-\udfff]|\ud86e[\udc00-\udc1d]|[\u3041-\u3096]|[\u30A1-\u30FA]/iu

services/flexsearchSvc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ function getMatch(page, query, terms) {
170170
}
171171

172172
function getHeaderMatch(page, term) {
173+
// if (page.slug && page.slug.includes('versioning'))
174+
console.log(page)
173175
if (!page.headers) return null
174176
for (let i = 0; i < page.headers.length; i++) {
175177
const h = page.headers[i]

0 commit comments

Comments
 (0)