1
1
const { path } = require ( '@vuepress/shared-utils' )
2
2
const htmlToText = require ( 'html-to-text' )
3
+ const _ = require ( 'lodash' )
3
4
4
- module . exports = options => ( {
5
+ let customTitles = null
6
+
7
+ module . exports = ( options , ctx , globalCtx ) => ( {
5
8
extendPageData ( $page ) {
6
9
try {
7
10
const { html } = $page . _context . markdown . render ( $page . _strippedContent || '' )
11
+ if ( ! customTitles ) customTitles = getCustomTitles ( globalCtx )
8
12
9
13
const plaintext = htmlToText . fromString ( html , {
10
14
wordwrap : null ,
@@ -24,6 +28,9 @@ module.exports = options => ({
24
28
$page . content = plaintext
25
29
$page . contentLowercase = plaintext . toLowerCase ( )
26
30
$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 ) ]
27
34
} catch ( e ) {
28
35
// incorrect markdown
29
36
console . error ( 'Error when applying fulltext-search plugin:' , e )
@@ -40,6 +47,60 @@ module.exports = options => ({
40
47
} ,
41
48
} )
42
49
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
+
43
104
function getCharsets ( text ) {
44
105
const cyrillicRegex = / [ \u0400 - \u04FF ] / iu
45
106
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
0 commit comments