1
1
// Imports
2
2
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"
4
4
import remarkDirective from "remark-directive"
5
5
import { visit } from "unist-util-visit"
6
6
export { h } from "hastscript"
7
- import type { Data , ElementContent , Properties } from "hast"
8
7
9
8
/**
10
9
* Add support for custom directives.
@@ -38,7 +37,7 @@ export default {
38
37
* `.trim())
39
38
* ```
40
39
*/
41
- export function directive ( callback : ( node : AugmentedNode ) => void ) : Plugin {
40
+ export function directive ( callback : ( node : AstNode ) => void ) : Plugin {
42
41
return {
43
42
remark ( processor ) {
44
43
return processor . use ( function ( ) {
@@ -47,52 +46,27 @@ export function directive(callback: (node: AugmentedNode) => void): Plugin {
47
46
if ( ! / (?: c o n t a i n e r | l e a f | t e x t ) D i r e c t i v e / . test ( node . type ) ) {
48
47
return
49
48
}
50
- callback ( node as AugmentedNode )
49
+ callback ( node as AstNode )
51
50
} )
52
51
}
53
52
} )
54
53
} ,
55
54
} as Plugin
56
55
}
57
56
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
72
61
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 {
90
64
/**
91
65
* Signal that a node should result in something with these children.
92
66
*
93
67
* When this is defined, when a parent is created, these children will be used.
94
68
*/
95
- hChildren ?: ElementContent [ ]
69
+ hChildren ?: AstElementContent [ ]
96
70
97
71
/**
98
72
* Signal that a node should result in a particular element, instead of its default behavior.
@@ -107,5 +81,136 @@ export interface AugmentedData extends Data {
107
81
*
108
82
* When this is defined, when an element is created, these properties will be used.
109
83
*/
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"
111
216
}
0 commit comments