1+ let Glossary = {
2+ create : ( config ) => {
3+ this . config = config ;
4+
5+ let loadGlossary = async ( ) => {
6+ return fetch ( '_glossary.md' )
7+ . then ( function ( data ) {
8+ return data . text ( ) ;
9+ } )
10+ . then ( function ( text ) {
11+ window . $docsify . terms = { } ;
12+
13+ let termTexts = text . split ( '#####' ) ;
14+ termTexts . forEach ( ( termText ) => {
15+ if ( termText . length <= 5 ) { // If it can't possibly contain a term, skip
16+ return ;
17+ }
18+
19+ // Parse the term and its content
20+ let title = termText . split ( '\n' , 1 ) [ 0 ] . trim ( ) ;
21+ let id = title . toLowerCase ( ) . replace ( ' ' , '-' ) ;
22+ let content = termText . substr ( termText . indexOf ( '\n' ) + 1 ) . trim ( ) ;
23+ content = content . replace ( "\r\n\r\n" , "\n" ) ;
24+ if ( content . length > 200 ) {
25+ content = content . substring ( 0 , 200 ) + "..." ;
26+ }
27+ window . $docsify . terms [ title ] = {
28+ id : id ,
29+ content : content
30+ } ;
31+ } ) ;
32+ } ) ;
33+ } ;
34+
35+ let addLinks = ( content , next , terms ) => {
36+ for ( let term in terms ) {
37+ console . log ( term ) ;
38+
39+ let regex = new RegExp ( `\\b${ term } \\b` , 'ig' ) ;
40+ content = content . replace ( regex , ( match ) => {
41+ let termData = terms [ term ] ;
42+ return `[${ match } *](/_glossary?id=${ termData . id } "${ termData . content } ")` ;
43+ } ) ;
44+ }
45+ next ( content ) ;
46+ } ;
47+
48+ return ( hook , vm ) => {
49+ hook . beforeEach ( function ( content , next )
50+ {
51+ if ( window . location . hash . match ( / _ g l o s s a r y / g) ) {
52+ next ( content ) ;
53+ return ;
54+ }
55+
56+ if ( ! window . $docsify . terms ) {
57+ // Glossary needs loading before creating the link
58+ loadGlossary ( ) . then ( ( ) => {
59+ addLinks ( content , next , window . $docsify . terms ) ;
60+ } )
61+ } else {
62+ addLinks ( content , next , window . $docsify . terms ) ;
63+ }
64+ } ) ;
65+ } ;
66+ }
67+ } ;
0 commit comments