Skip to content

Commit 2b7057a

Browse files
committed
Update to meilisearch, improve markdown rendering for search
1 parent 43c4ff5 commit 2b7057a

File tree

6 files changed

+85
-62
lines changed

6 files changed

+85
-62
lines changed

package-lock.json

Lines changed: 9 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"js-yaml": "^3.13.1",
3838
"list-item": "^2.0.0",
3939
"markdown-slug": "^0.1.1",
40-
"marked": "^0.7.0",
40+
"marked": "^7.0.5",
4141
"postcss": "^8.4.18",
4242
"replace-ext": "^1.0.0",
4343
"tailwindcss": "^3.2.1",

scripts/md-render.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Fs = require('fs')
1111
const Path = require('path')
1212
const MarkedJs = require('marked')
1313

14+
1415
const isUrl = require('is-url')
1516
const { bsToFs } = require('./utility.js')
1617
const Logger = require('./logger.js')
@@ -185,13 +186,56 @@ renderer.table = function(header, body) {
185186
return `<div class="table-wrapper"><table><thead>${header}</thead>${body}</table></div>`
186187
}
187188

189+
190+
// The sectionLevel will help us prevent matching the same header multiple times.
191+
let sectionLevel = 0;
192+
193+
// Creating regular expressions is expensive so we create them once.
194+
// Create 7 sections since that is the maximum heading level.
195+
const sectionRegexps = new Array(7).fill().map((e, i) => new RegExp(`^(#{${i + 1}} )[^]*?(?:\\n(?=\\1)|$)`));
196+
197+
const sectionExtension = {
198+
name: 'sectionBlock',
199+
level: 'block',
200+
start(src) {
201+
// Match when # is at the beginning of a line.
202+
return src.match(/^#/m)?.index;
203+
},
204+
tokenizer(src) {
205+
const match = src.match(sectionRegexps[sectionLevel]);
206+
if (!match) {
207+
return;
208+
}
209+
210+
211+
sectionLevel++;
212+
// Tokenize text inside the section.
213+
// Only add sectionBlock token for headers one level up from current level.
214+
const tokens = this.lexer.blockTokens(match[0]);
215+
sectionLevel--;
216+
217+
return {
218+
type: 'sectionBlock',
219+
raw: match[0],
220+
level: sectionLevel + 1,
221+
tokens
222+
};
223+
},
224+
renderer(token) {
225+
const tag = token.level === 1 ? 'article' : 'section';
226+
return `<${tag}>\n${this.parser.parse(token.tokens)}</${tag}>\n`;
227+
}
228+
};
229+
188230
// -------------------------------------------------------------------
189231

190232
module.exports = function (text, info) {
191233
currentPageInfo = info
192234
tocGenerator = new PageTocGenerator()
193235

194-
let renderedContent = MarkedJs(text, { renderer: renderer })
236+
MarkedJs.marked.use({ renderer: renderer, extensions: [sectionExtension] })
237+
let renderedContent = MarkedJs.marked.parse(text)
238+
195239

196240
renderedContent = tocGenerator.renderTocs(renderedContent, currentPageInfo)
197241

scripts/page-toc-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class PageTocGenerator {
102102

103103
let start = toc.hide ? '<div class="table-of-contents collapse"><div class="toc-overview-button">Show List</div>' : '<div class="table-of-contents">'
104104
// Render the list as html
105-
let tocContent = start + Marked(list) + '</div>'
105+
let tocContent = start + Marked.marked.parse(list) + '</div>'
106106
// Replace the temporary id with the new html
107107
pageContent = pageContent.replace(key, tocContent)
108108
}

theme/doc-page-template.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<title>{{page_title}} — ExpressionEngine {{current_version}} Documentation</title>
1616

17-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@docsearch/css@3" />
17+
<link rel="stylesheet" href="https://unpkg.com/meilisearch-docsearch@latest/dist/index.css"/>
1818

1919
<link rel="stylesheet" href="{{root_dir}}_assets/default.min.css">
2020

@@ -93,7 +93,7 @@
9393
<div class="sidebar-container">
9494
<div class="sidebar">
9595
<div class="sidebar-search" id="docsearch">
96-
96+
9797
</div>
9898

9999
<div class="sidebar-toc">
@@ -127,20 +127,20 @@ <h2>Community</h2>
127127
</div>
128128
</section>
129129

130-
<script src="https://cdn.jsdelivr.net/npm/@docsearch/js@3"></script>
131-
<script type="text/javascript">
132-
133-
docsearch({
134-
appId: 'BFKETQLPH4',
135-
apiKey: '871012547f7adf3ab753e99d158612f6',
136-
indexName: 'expressionengine',
137-
container: '#docsearch',
138-
searchParameters: {
139-
'facetFilters': ["version:latest"],
140-
},
141-
debug: false
142-
});
143-
</script>
130+
<script src="https://unpkg.com/meilisearch-docsearch@latest/dist/index.global.js"></script>
131+
<script>
132+
const { docsearch } = window.__docsearch_meilisearch__;
133+
docsearch({
134+
container: "#docsearch",
135+
host: "https://docsearch.expressionengine.com",
136+
apiKey: "{{docsearch_public_key}}",
137+
indexUid: "{{docsearch_index}}",
138+
searchParams: {
139+
distinct: "hierarchy_lvl1",
140+
attributesToSearchOn: ["content","hierarchy_lvl1"],
141+
}
142+
});
143+
</script>
144144

145145
<script src="{{root_dir}}_assets/main.min.js"></script>
146146
</body>

theme/site-template.html

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,17 @@ <h2>Community</h2>
8181
</div>
8282

8383
<script type="text/javascript">
84-
docsearch({
85-
apiKey: 'ba20cc61ab2d8a0148041c98eeddd9c6',
86-
indexName: 'expressionengine',
87-
inputSelector: '#docsearch-input',
88-
algoliaOptions: {
89-
'facetFilters': ["version:latest"],
90-
'hitsPerPage': 50
91-
},
92-
debug: false
93-
});
94-
docsearch({
95-
apiKey: 'ba20cc61ab2d8a0148041c98eeddd9c6',
96-
indexName: 'expressionengine',
97-
inputSelector: '#docsearch-input-2',
98-
algoliaOptions: {
99-
'facetFilters': ["version:latest"],
100-
'hitsPerPage': 50
101-
},
102-
debug: false
103-
});
84+
const { docsearch } = window.__docsearch_meilisearch__;
85+
docsearch({
86+
container: "#docsearch-input",
87+
host: "https://docsearch.expressionengine.com",
88+
apiKey: "{{docsearch_public_key}}",
89+
indexUid: "{{docsearch_index}}",
90+
});
91+
docsearch({
92+
container: "#docsearch-input2",
93+
host: "https://docsearch.expressionengine.com",
94+
apiKey: "{{docsearch_public_key}}",
95+
indexUid: "{{docsearch_index}}",
96+
});
10497
</script>

0 commit comments

Comments
 (0)