Skip to content

Commit e05b7e4

Browse files
authored
fix(astro-fuse): Correct frontmatter parsing on source mode (#32)
1 parent 5396966 commit e05b7e4

File tree

4 files changed

+27
-576
lines changed

4 files changed

+27
-576
lines changed

packages/astro-fuse/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# astro-fuse
22

3+
## 1.0.4
4+
5+
### Patch Changes
6+
7+
- Correct frontmatter parsing on source mode
8+
39
## 1.0.3
410

511
### Patch Changes

packages/astro-fuse/package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "astro-fuse",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Use Fuse.js to search documents in your Astro site",
55
"keywords": [
66
"astro-integration",
@@ -40,13 +40,7 @@
4040
"fuse.js": "^7.0.0",
4141
"html-to-text": "^9.0.5",
4242
"js-yaml": "^4.1.0",
43-
"lodash.debounce": "^4.0.8",
44-
"mdast-util-from-markdown": "^1.3.0",
45-
"mdast-util-frontmatter": "^1.0.1",
46-
"mdast-util-mdx": "^2.0.1",
47-
"mdast-util-to-markdown": "^1.5.0",
48-
"micromark-extension-frontmatter": "^1.0.1",
49-
"micromark-extension-mdxjs": "^1.0.0"
43+
"lodash.debounce": "^4.0.8"
5044
},
5145
"devDependencies": {
5246
"@eslint/js": "^9.7.0",

packages/astro-fuse/src/basedOnSource.ts

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import { AstroConfig } from 'astro'
44
import Fuse, { type FuseOptionKey } from 'fuse.js'
55
import { load } from 'js-yaml'
66
import debounce from 'lodash.debounce'
7-
import { fromMarkdown } from 'mdast-util-from-markdown'
8-
import { frontmatterFromMarkdown } from 'mdast-util-frontmatter'
9-
import { mdxFromMarkdown, mdxToMarkdown } from 'mdast-util-mdx'
10-
import { toMarkdown } from 'mdast-util-to-markdown'
11-
import { frontmatter } from 'micromark-extension-frontmatter'
12-
import { mdxjs } from 'micromark-extension-mdxjs'
137
import { createHmac } from 'node:crypto'
148
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
159
import { readFile, writeFile } from 'node:fs/promises'
@@ -110,10 +104,11 @@ export function basedOnSource({
110104
}
111105

112106
const { fileId, fileUrl } = getFileInfo(id, config)
113-
const code = await readFile(fileId, 'utf-8')
114-
const hash = createHmac('sha256', code).digest('hex').substring(0, 8)
115-
const content = getSearchable(code)
116-
buffer.set(fileId, [hash, { ...content, fileUrl }])
107+
const content = await readFile(fileId, 'utf-8')
108+
const hash = createHmac('sha256', content).digest('hex').substring(0, 8)
109+
const frontmatter = getFrontmatter(content)
110+
111+
buffer.set(fileId, [hash, { content, fileUrl, frontmatter }])
117112

118113
writeFuseIndex()
119114
},
@@ -160,29 +155,20 @@ export function getFileInfo(id: string, config: AstroConfig): FileInfo {
160155
return { fileId, fileUrl }
161156
}
162157

163-
export function getSearchable(
164-
source: string
165-
): Omit<SourceBaseSearchable, 'fileUrl'> {
166-
const tree = fromMarkdown(source, 'utf-8', {
167-
extensions: [mdxjs(), frontmatter(['yaml'])],
168-
mdastExtensions: [
169-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
170-
mdxFromMarkdown() as any,
171-
frontmatterFromMarkdown(['yaml']),
172-
],
173-
})
174-
175-
const yaml = tree.children.splice(
176-
tree.children.findIndex((value) => value.type === 'yaml'),
177-
1
178-
)
158+
const FRONTMATTER_REGEX = /^---\s*([\s\S]*?)\s*---/
179159

180-
return {
181-
content: toMarkdown(tree, { extensions: [mdxToMarkdown()] }),
182-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
183-
frontmatter: load((yaml as any)?.[0]?.value ?? '') as Record<
184-
string,
185-
string
186-
>,
160+
export function getFrontmatter(source: string): Record<string, string> {
161+
const match = FRONTMATTER_REGEX.exec(source)
162+
163+
if (!match) {
164+
return {}
187165
}
166+
167+
const [, value] = match
168+
169+
if (!value) {
170+
return {}
171+
}
172+
173+
return load(value) as Record<string, string>
188174
}

0 commit comments

Comments
 (0)