@@ -4,12 +4,6 @@ import { AstroConfig } from 'astro'
44import Fuse , { type FuseOptionKey } from 'fuse.js'
55import { load } from 'js-yaml'
66import 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'
137import { createHmac } from 'node:crypto'
148import { existsSync , mkdirSync , writeFileSync } from 'node:fs'
159import { 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