MCPcopy Create free account
hub / github.com/SyntaxUI/syntaxui / Search

Function Search

src/mdx/search.mjs:47–135  ·  view source on GitHub ↗
(nextConfig = {})

Source from the content-addressed store, hash-verified

45}
46
47export default function Search(nextConfig = {}) {
48 let cache = new Map()
49
50 return Object.assign({}, nextConfig, {
51 webpack(config, options) {
52 config.module.rules.push({
53 test: __filename,
54 use: [
55 createLoader(function () {
56 let appDir = path.resolve('./src/app')
57 this.addContextDependency(appDir)
58
59 let files = glob.sync('**/*.mdx', { cwd: appDir })
60 let data = files.map((file) => {
61 let url = '/' + file.replace(/(^|\/)page\.mdx$/, '')
62 let mdx = fs.readFileSync(path.join(appDir, file), 'utf8')
63
64 let sections = []
65
66 if (cache.get(file)?.[0] === mdx) {
67 sections = cache.get(file)[1]
68 } else {
69 let vfile = { value: mdx, sections }
70 processor.runSync(processor.parse(vfile), vfile)
71 cache.set(file, [mdx, sections])
72 }
73
74 return { url, sections }
75 })
76
77 // When this file is imported within the application
78 // the following module is loaded:
79 return `
80 import FlexSearch from 'flexsearch'
81
82 let sectionIndex = new FlexSearch.Document({
83 tokenize: 'full',
84 document: {
85 id: 'url',
86 index: 'content',
87 store: ['title', 'pageTitle'],
88 },
89 context: {
90 resolution: 9,
91 depth: 2,
92 bidirectional: true
93 }
94 })
95
96 let data = ${JSON.stringify(data)}
97
98 for (let { url, sections } of data) {
99 for (let [title, hash, content] of sections) {
100 sectionIndex.add({
101 url: url + (hash ? ('#' + hash) : ''),
102 title,
103 content: [title, ...content].join('\\n'),
104 pageTitle: hash ? sections[0][0] : undefined,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected