1313 */
1414
1515import { hasProperty } from 'hast-util-has-property'
16+ // @ts -expect-error: next.
1617import minifyWhitespace from 'rehype-minify-whitespace'
1718import { convert } from 'unist-util-is'
1819import { visit } from 'unist-util-visit'
@@ -50,7 +51,7 @@ export function toMdast(tree, options = {}) {
5051 * @param {string|Array.<Node> } [children]
5152 */
5253 ( node , type , props , children ) => {
53- /** @type {Properties } */
54+ /** @type {Properties|undefined } */
5455 let properties
5556
5657 if ( typeof props === 'string' || Array . isArray ( props ) ) {
@@ -61,7 +62,7 @@ export function toMdast(tree, options = {}) {
6162 }
6263
6364 /** @type {Node } */
64- // @ts -ignore Assume valid `type` and `children`/`value`.
65+ // @ts -expect-error Assume valid `type` and `children`/`value`.
6566 const result = { type, ...properties }
6667
6768 if ( typeof children === 'string' ) {
@@ -98,7 +99,7 @@ export function toMdast(tree, options = {}) {
9899
99100 minifyWhitespace ( { newlines : options . newlines === true } ) ( tree )
100101
101- const result = one ( h , tree , null )
102+ const result = one ( h , tree , undefined )
102103
103104 if ( ! result ) {
104105 mdast = { type : 'root' , children : [ ] }
@@ -115,6 +116,7 @@ export function toMdast(tree, options = {}) {
115116 /** @type {import('unist-util-visit').Visitor<Element> } */
116117 function onelement ( node ) {
117118 const id =
119+ // @ts -expect-error: `hasProperty` just checked it.
118120 hasProperty ( node , 'id' ) && String ( node . properties . id ) . toUpperCase ( )
119121
120122 if ( id && ! own . call ( byId , id ) ) {
@@ -132,11 +134,15 @@ export function toMdast(tree, options = {}) {
132134 * @type {import('unist-util-visit').Visitor<Text> }
133135 */
134136 function ontext ( node , index , parent ) {
137+ /* c8 ignore next 3 */
138+ if ( index === null || ! parent ) {
139+ return
140+ }
141+
135142 const previous = parent . children [ index - 1 ]
136143
137- if ( previous && node . type === previous . type ) {
144+ if ( previous && previous . type === node . type ) {
138145 previous . value += node . value
139-
140146 parent . children . splice ( index , 1 )
141147
142148 if ( previous . position && node . position ) {
@@ -151,7 +157,7 @@ export function toMdast(tree, options = {}) {
151157
152158 // We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
153159 // as there the whitespace matters.
154- if ( block ( parent ) ) {
160+ if ( parent && block ( parent ) ) {
155161 if ( ! index ) {
156162 node . value = node . value . replace ( / ^ [ \t ] + / , '' )
157163 }
0 commit comments