forked from jake-low/remark-sectionize
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Convert to TypeScript, PNPM, add tsup, update configs (#2)
- Loading branch information
1 parent
d559774
commit 20b015b
Showing
14 changed files
with
3,610 additions
and
2,522 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules/ | ||
.DS_Store | ||
dist/ |
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,3 @@ | ||
{ | ||
"editor.defaultFormatter": "biomejs.biome" | ||
} |
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,32 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json", | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true, | ||
"suspicious": { | ||
"noExplicitAny": "warn" | ||
}, | ||
"complexity": { | ||
"useArrowFunction": "info" | ||
} | ||
} | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "space", | ||
"formatWithErrors": true | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"trailingCommas": "es5", | ||
"bracketSameLine": true | ||
} | ||
}, | ||
"files": { | ||
"ignore": ["dist", ".astro", "node_modules"] | ||
} | ||
} |
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,71 @@ | ||
import { findAfter } from "unist-util-find-after"; | ||
import { visit, type BuildVisitor } from "unist-util-visit"; | ||
import type { Node, Parent } from "unist"; | ||
import type { Transformer } from "unified"; | ||
|
||
const MAX_HEADING_DEPTH = 6; | ||
|
||
interface HeadingNode extends Node { | ||
type: "heading"; | ||
depth: 1 | 2 | 3 | 4 | 5 | 6; | ||
} | ||
|
||
interface SectionNode extends Parent { | ||
type: "section"; | ||
depth: number; | ||
data: { | ||
hName: "section"; | ||
}; | ||
} | ||
|
||
type GenericNode = Node | HeadingNode | SectionNode; | ||
|
||
function plugin(): Transformer<GenericNode, GenericNode> { | ||
return transform; | ||
} | ||
type Test = (node: GenericNode) => boolean; | ||
|
||
function transform(tree: GenericNode): GenericNode { | ||
for (let depth = MAX_HEADING_DEPTH; depth > 0; depth--) { | ||
visit( | ||
tree, | ||
(node): node is HeadingNode => | ||
node.type === "heading" && | ||
"depth" in node && | ||
typeof node.depth === "number" && | ||
node.depth === depth, | ||
sectionize as unknown as BuildVisitor<GenericNode, Test> | ||
); | ||
} | ||
return tree; | ||
} | ||
|
||
function sectionize(node: HeadingNode, index: number, parent: Parent): void { | ||
const start = node; | ||
const startIndex = index; | ||
const depth = start.depth; | ||
|
||
const isEnd = (node: GenericNode): boolean => | ||
(node.type === "heading" && "depth" in node && node.depth <= depth) || | ||
node.type === "export"; | ||
const end = findAfter(parent, start, isEnd); | ||
const endIndex = end ? parent.children.indexOf(end) : parent.children.length; | ||
|
||
const between = parent.children.slice( | ||
startIndex, | ||
endIndex > 0 ? endIndex : undefined | ||
); | ||
|
||
const section: SectionNode = { | ||
type: "section", | ||
depth: depth, | ||
children: between, | ||
data: { | ||
hName: "section", | ||
}, | ||
}; | ||
|
||
parent.children.splice(startIndex, section.children.length, section); | ||
} | ||
|
||
export default plugin; |
Oops, something went wrong.