Skip to content

Commit d3c2ec8

Browse files
committedNov 29, 2024··
fix(markdown): remove dependecy on depreacted hast
1 parent 1f52e77 commit d3c2ec8

File tree

5 files changed

+145
-52
lines changed

5 files changed

+145
-52
lines changed
 

‎deno.lock

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎markdown/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"dependencies": {
1414
"@std/text": "npm:@jsr/std__text@^1.0.8",
1515
"@std/yaml": "npm:@jsr/std__yaml@^1.0.5",
16-
"hast": "^1.0.0",
1716
"hastscript": "^9.0.0",
1817
"playwright": "^1.49.0",
1918
"rehype": "^13.0.2",

‎markdown/plugins/directives.ts

+142-37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Imports
22
import type { Plugin } from "../renderer.ts"
3-
import type { Arg, Nullable, Optional } from "@libs/typing/types"
3+
import type { Arg, Arrayable, Nullable, Optional } from "@libs/typing/types"
44
import remarkDirective from "remark-directive"
55
import { visit } from "unist-util-visit"
66
export { h } from "hastscript"
7-
import type { Data, ElementContent, Properties } from "hast"
87

98
/**
109
* Add support for custom directives.
@@ -38,7 +37,7 @@ export default {
3837
* `.trim())
3938
* ```
4039
*/
41-
export function directive(callback: (node: AugmentedNode) => void): Plugin {
40+
export function directive(callback: (node: AstNode) => void): Plugin {
4241
return {
4342
remark(processor) {
4443
return processor.use(function () {
@@ -47,52 +46,27 @@ export function directive(callback: (node: AugmentedNode) => void): Plugin {
4746
if (!/(?:container|leaf|text)Directive/.test(node.type)) {
4847
return
4948
}
50-
callback(node as AugmentedNode)
49+
callback(node as AstNode)
5150
})
5251
}
5352
})
5453
},
5554
} as Plugin
5655
}
5756

58-
/**
59-
* Node.
60-
*
61-
* Augmented from {@link https://github.com/syntax-tree/mdast-util-directive/blob/main/index.d.ts | mdast-util-directive}.
62-
*/
63-
export interface AugmentedNode extends Arg<typeof visit> {
64-
/**
65-
* Directive name.
66-
*/
67-
name: string
68-
/**
69-
* Directive attributes.
70-
*/
71-
attributes?: Nullable<Record<string, Optional<Nullable<string>>>>
57+
// =======================================================================================================
58+
// The following definitions were adapted from:
59+
// - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a6e9e491ff5d0fd7f438fb77a70b54b28e356ced/types/hast/v2/index.d.ts
60+
// - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a6e9e491ff5d0fd7f438fb77a70b54b28e356ced/types/unist/v2/index.d.ts
7261

73-
/**
74-
* Info from the ecosystem.
75-
*/
76-
data?: AugmentedData
77-
78-
/**
79-
* Children of the directive.
80-
*/
81-
children: AugmentedNode[]
82-
}
83-
84-
/**
85-
* Info associated with hast nodes by the ecosystem.
86-
*
87-
* Augmented from {@link https://github.com/syntax-tree/mdast-util-to-hast/blob/main/index.d.ts | mdast-util-to-hast}.
88-
*/
89-
export interface AugmentedData extends Data {
62+
/** Information associated by the ecosystem with the node. */
63+
export interface AstData {
9064
/**
9165
* Signal that a node should result in something with these children.
9266
*
9367
* When this is defined, when a parent is created, these children will be used.
9468
*/
95-
hChildren?: ElementContent[]
69+
hChildren?: AstElementContent[]
9670

9771
/**
9872
* Signal that a node should result in a particular element, instead of its default behavior.
@@ -107,5 +81,136 @@ export interface AugmentedData extends Data {
10781
*
10882
* When this is defined, when an element is created, these properties will be used.
10983
*/
110-
hProperties?: Properties
84+
hProperties?: AstProperties
85+
86+
/** Represents information associated with an element. */
87+
[key: string]: unknown
88+
}
89+
90+
/** Represents information associated with an element. */
91+
export interface AstProperties {
92+
/** Represents information associated with an element. */
93+
[PropertyName: string]: Optional<Nullable<boolean | Arrayable<string | number>>>
94+
}
95+
96+
/** Syntactic units in unist syntax trees are called nodes. */
97+
export interface AstNode<Data extends object = AstData, ChildNode = unknown> {
98+
/** The variant of a node. */
99+
type: string
100+
/** Information from the ecosystem. */
101+
data?: Data
102+
/** Location of a node in a source document. Must not be present if a node is generated. */
103+
position?: AstPosition
104+
/** Directive name. */
105+
name?: string
106+
/** Directive attributes. */
107+
attributes?: Nullable<Record<string, Optional<Nullable<string>>>>
108+
/** Children of the directive. */
109+
children: ChildNode[]
110+
}
111+
112+
/** Util for extracting type of {@link Node.data}. */
113+
export type AstNodeData<Node extends AstNode<object>> = Node extends AstNode<infer TData> ? TData : never
114+
115+
/** Location of a node in a source file. */
116+
export interface AstPosition {
117+
/** Place of the first character of the parsed source region. */
118+
start: AstPoint
119+
/** Place of the first character after the parsed source region. */
120+
end: AstPoint
121+
/** Start column at each index (plus start line) in the source region, for elements that span multiple lines. */
122+
indent?: number[]
123+
}
124+
125+
/** One place in a source file. */
126+
export interface AstPoint {
127+
/** Line in a source file (1-indexed integer). */
128+
line: number
129+
/** Column in a source file (1-indexed integer). */
130+
column: number
131+
/** Character in a source file (0-indexed integer). */
132+
offset?: number
133+
}
134+
135+
/** Node in hast containing other nodes. */
136+
export interface AstParent<ChildNode extends AstNode<object> = AstContent, Data extends object = AstNodeData<ChildNode>> extends AstNode<Data> {
137+
/** List representing the children of a node. */
138+
children: ChildNode[]
139+
}
140+
141+
/** Represents a root or element node. */
142+
export type AstContent = AstRootContent | AstElementContent
143+
144+
/** Represents a root node content. */
145+
export type AstRootContent = AstRootContentMap[keyof AstRootContentMap]
146+
147+
/** Represents an element node content. */
148+
export type AstElementContent = AstElementContentMap[keyof AstElementContentMap]
149+
150+
/** Represents an HTML DocumentType. */
151+
export interface AstDocType extends AstNode {
152+
/** Represents this variant of a Node. */
153+
type: "doctype"
154+
/** Represents the document name. */
155+
name: string
156+
}
157+
158+
/**
159+
* Root represents a document.
160+
* Can be used as the root of a tree, or as a value of the content field on a 'template' Element, never as a child.
161+
*/
162+
export interface AstRoot extends AstParent {
163+
/** Represents this variant of a Node. */
164+
type: "root"
165+
/** List representing the children of a node. */
166+
children: AstRootContent[]
167+
}
168+
169+
/** This map registers all node types that may be used as top-level content in the document. These types are accepted inside `root` nodes. */
170+
export interface AstRootContentMap extends AstElementContentMap {
171+
/** Represents an HTML DocumentType. */
172+
doctype: AstDocType
173+
}
174+
175+
/** Element represents an HTML Element. */
176+
export interface AstElement extends AstParent {
177+
/** Represents this variant of a Node. */
178+
type: "element"
179+
/** Represents the element’s local name. */
180+
tagName: string
181+
/** Represents information associated with the element. */
182+
properties?: AstProperties
183+
/** If the tagName field is 'template', a content field can be present. */
184+
content?: AstRoot
185+
/** List representing the children of a node. */
186+
children: AstElementContent[]
187+
}
188+
189+
/** This map registers all node types that may be used as content in an element. These types are accepted inside `element` nodes. */
190+
export interface AstElementContentMap {
191+
/** Represents an HTML Element. */
192+
// deno-lint-ignore no-explicit-any
193+
element: any // AstElement
194+
/** Represents an HTML Comment. */
195+
comment: AstComment
196+
/** Represents an HTML Text. */
197+
text: AstText
198+
}
199+
200+
/** Nodes in hast containing a value. */
201+
export interface AstLiteral extends AstNode {
202+
/** Represents the value of a node. */
203+
value: string
204+
}
205+
206+
/** Represents an HTML Comment. */
207+
export interface AstComment extends AstLiteral {
208+
/** Represents this variant of a Literal. */
209+
type: "comment"
210+
}
211+
212+
/** Represents an HTML Text. */
213+
export interface AstText extends AstLiteral {
214+
/** Represents this variant of a Literal. */
215+
type: "text"
111216
}

‎markdown/plugins/mod.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
export type { Arg, Nullable, Optional } from "@libs/typing/types"
1+
export type { Arg, Arrayable, Nullable, Optional } from "@libs/typing/types"
22
export { default as anchors } from "./anchors.ts"
3-
export { type AugmentedData, type AugmentedNode, default as directives, directive, h } from "./directives.ts"
3+
export { default as directives, directive, h } from "./directives.ts"
4+
export type * from "./directives.ts"
45
export { default as emojis } from "./emojis.ts"
56
export { default as frontmatter } from "./frontmatter.ts"
67
export { default as gfm } from "./gfm.ts"

‎package-lock.json

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.