-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jast-util-to-texast.ts
executable file
·126 lines (109 loc) · 3.33 KB
/
jast-util-to-texast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { one } from './one.js'
import { handlers } from './handlers/index.js'
import {
Context,
J,
JWithoutProps,
JWithProps,
TexastContent,
TexastRoot,
Node,
Options,
Attributes,
} from './types.js'
import { convert } from 'unist-util-is'
import rehypeMinifyWhitespace from 'rehype-minify-whitespace'
export { one } from './one.js'
export { all } from './all.js'
export { handlers as defaultHandlers }
const block = convert(['heading', 'paragraph', 'root'])
export function toTexast(
tree: TexastRoot | TexastContent,
options: Options = {
newLines: false,
checked: '[x]',
unchecked: '[ ]',
quotes: ['"'],
topSection: 1,
columnSeparator: false,
},
) {
// const byId: { [s: string]: Element } = {}
let texast: TexastContent | TexastRoot
// TODO: fix this type error
const j: J = Object.assign(
((
node: TexastRoot | TexastContent,
type: string,
props?: Attributes | string | TexastContent[],
children?: string | TexastContent[],
) => {
let attributes: Attributes | undefined
if (typeof props === 'string' || Array.isArray(props)) {
children = props
attributes = {}
} else {
attributes = props
}
// @ts-expect-error Assume valid `type` and `children`/`value`.
const result: Node = { type, ...attributes }
if (typeof children === 'string') {
// @ts-expect-error: Looks like a literal.
result.value = children
} else if (children) {
// @ts-expect-error: Looks like a parent.
result.children = children
}
if (node.position) {
result.position = node.position
}
return result as TexastContent
}) as JWithProps & JWithoutProps,
{
// nodeById: byId,
baseFound: false,
inTable: false,
wrapText: true,
/** @type {string|null} */
frozenBaseUrl: null,
qNesting: 0,
handlers: options.handlers ? { ...handlers, ...options.handlers } : handlers,
document: options.document,
checked: options.checked || '[x]',
unchecked: options.unchecked || '[ ]',
quotes: options.quotes || ['"'],
italics: options.italics || 'emph',
sectionDepth: options.topSection || 1,
documentClass: options.documentClass || { name: 'article' },
bibname: options.bibname || 'bibliography',
columnSeparator: !!options.columnSeparator,
footnotes: {},
booktabs: options.booktabs ?? true,
rowNumber: 0,
numberOfRows: 0,
citationAnalyzer: options.citationAnalyzer || ((node: Node) => 'autocite'),
} as Context,
)
// visit(tree, 'element', (node) => {
// const id =
// node.attributes &&
// 'id' in node.attributes &&
// String(node.attributes.id).toUpperCase()
// if (id && !own.call(byId, id)) {
// byId[id] = node
// }
// })
// @ts-expect-error: does return a transformer, that does accept any node.
rehypeMinifyWhitespace({ newlines: options.newlines === true })(tree)
// @ts-expect-error: does return a transformer, that does accept any node.
const result = one(j, tree, undefined)
if (!result) {
texast = { type: 'root', children: [] }
} else if (Array.isArray(result)) {
texast = { type: 'root', children: result }
} else {
texast = result
}
// visit(mdast, 'text', ontext)
return texast
}