-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from mrjvs/dev
Guider v1.0.2
- Loading branch information
Showing
5 changed files
with
99 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { SKIP, visit } from 'unist-util-visit'; | ||
import type { Heading, Root } from 'mdast'; | ||
import { phrasing } from 'mdast-util-phrasing'; | ||
import type { VFileWithOutput } from 'unified'; | ||
|
||
interface Section { | ||
heading?: { id: string; depth: Heading['depth']; text: string }; | ||
content: string; | ||
} | ||
|
||
declare module 'mdast' { | ||
interface HeadingData { | ||
id: string; | ||
} | ||
} | ||
|
||
export function remarkSearchData() { | ||
return (root: Root, vFile: VFileWithOutput<any>) => { | ||
const sections: Section[] = []; | ||
|
||
let currentSection: Section | undefined; | ||
let previousParentNode: any; | ||
|
||
visit(root, (node, _, parent) => { | ||
if (node.type === 'heading') { | ||
if (currentSection) { | ||
sections.push(currentSection); | ||
} | ||
|
||
const heading = node; | ||
const id = heading.data?.id ?? ''; | ||
const depth = heading.depth; | ||
let text = ''; | ||
visit(heading, ['text', 'inlineCode'], (hChild: any) => { | ||
text += hChild.value; | ||
}); | ||
|
||
currentSection = { | ||
heading: { | ||
id, | ||
depth, | ||
text, | ||
}, | ||
content: '', | ||
}; | ||
|
||
return SKIP; | ||
} | ||
|
||
if (node.type === 'text' || node.type === 'inlineCode') { | ||
if (!currentSection) { | ||
currentSection = { | ||
heading: undefined, | ||
content: '', | ||
}; | ||
} | ||
|
||
if ( | ||
previousParentNode && | ||
previousParentNode !== parent && | ||
!phrasing(previousParentNode) | ||
) { | ||
currentSection.content += ' '; | ||
} | ||
currentSection.content += node.value; | ||
|
||
previousParentNode = parent; | ||
} | ||
}); | ||
|
||
if (currentSection) sections.push(currentSection); | ||
|
||
vFile.data = { ...vFile.data, sections }; | ||
}; | ||
} |