10
10
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
11
11
* @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
12
12
* @typedef {import('mdast-util-to-markdown').Context } ToMarkdownContext
13
+ * @typedef {import('mdast-util-to-markdown').SafeOptions } SafeOptions
13
14
*
14
15
* @typedef Options
15
16
* @property {boolean } [tableCellPadding=true]
@@ -142,9 +143,12 @@ export function gfmTableToMarkdown(options) {
142
143
* @type {ToMarkdownHandle }
143
144
* @param {Table } node
144
145
*/
145
- function handleTable ( node , _ , context ) {
146
- // @ts -expect-error: fixed in `markdown-table@3.0.1`.
147
- return serializeData ( handleTableAsData ( node , context ) , node . align )
146
+ function handleTable ( node , _ , context , safeOptions ) {
147
+ return serializeData (
148
+ handleTableAsData ( node , context , safeOptions ) ,
149
+ // @ts -expect-error: fixed in `markdown-table@3.0.1`.
150
+ node . align
151
+ )
148
152
}
149
153
150
154
/**
@@ -155,8 +159,8 @@ export function gfmTableToMarkdown(options) {
155
159
* @type {ToMarkdownHandle }
156
160
* @param {TableRow } node
157
161
*/
158
- function handleTableRow ( node , _ , context ) {
159
- const row = handleTableRowAsData ( node , context )
162
+ function handleTableRow ( node , _ , context , safeOptions ) {
163
+ const row = handleTableRowAsData ( node , context , safeOptions )
160
164
// `markdown-table` will always add an align row
161
165
const value = serializeData ( [ row ] )
162
166
return value . slice ( 0 , value . indexOf ( '\n' ) )
@@ -166,10 +170,11 @@ export function gfmTableToMarkdown(options) {
166
170
* @type {ToMarkdownHandle }
167
171
* @param {TableCell } node
168
172
*/
169
- function handleTableCell ( node , _ , context ) {
173
+ function handleTableCell ( node , _ , context , safeOptions ) {
170
174
const exit = context . enter ( 'tableCell' )
171
175
const subexit = context . enter ( 'phrasing' )
172
176
const value = containerPhrasing ( node , context , {
177
+ ...safeOptions ,
173
178
before : around ,
174
179
after : around
175
180
} )
@@ -179,8 +184,8 @@ export function gfmTableToMarkdown(options) {
179
184
}
180
185
181
186
/**
182
- * @param {Array. <Array. <string>> } matrix
183
- * @param {Array. <string> } [align]
187
+ * @param {Array<Array<string>> } matrix
188
+ * @param {Array<string> } [align]
184
189
*/
185
190
function serializeData ( matrix , align ) {
186
191
return markdownTable ( matrix , {
@@ -194,16 +199,21 @@ export function gfmTableToMarkdown(options) {
194
199
/**
195
200
* @param {Table } node
196
201
* @param {ToMarkdownContext } context
202
+ * @param {SafeOptions } safeOptions
197
203
*/
198
- function handleTableAsData ( node , context ) {
204
+ function handleTableAsData ( node , context , safeOptions ) {
199
205
const children = node . children
200
206
let index = - 1
201
- /** @type {Array. <Array. <string>> } */
207
+ /** @type {Array<Array<string>> } */
202
208
const result = [ ]
203
209
const subexit = context . enter ( 'table' )
204
210
205
211
while ( ++ index < children . length ) {
206
- result [ index ] = handleTableRowAsData ( children [ index ] , context )
212
+ result [ index ] = handleTableRowAsData (
213
+ children [ index ] ,
214
+ context ,
215
+ safeOptions
216
+ )
207
217
}
208
218
209
219
subexit ( )
@@ -214,16 +224,25 @@ export function gfmTableToMarkdown(options) {
214
224
/**
215
225
* @param {TableRow } node
216
226
* @param {ToMarkdownContext } context
227
+ * @param {SafeOptions } safeOptions
217
228
*/
218
- function handleTableRowAsData ( node , context ) {
229
+ function handleTableRowAsData ( node , context , safeOptions ) {
219
230
const children = node . children
220
231
let index = - 1
221
- /** @type {Array. <string> } */
232
+ /** @type {Array<string> } */
222
233
const result = [ ]
223
234
const subexit = context . enter ( 'tableRow' )
224
235
225
236
while ( ++ index < children . length ) {
226
- result [ index ] = handleTableCell ( children [ index ] , node , context )
237
+ // Note: the positional info as used here is incorrect.
238
+ // Making it correct would be impossible due to aligning cells?
239
+ // And it would need copy/pasting `markdown-table` into this project.
240
+ result [ index ] = handleTableCell (
241
+ children [ index ] ,
242
+ node ,
243
+ context ,
244
+ safeOptions
245
+ )
227
246
}
228
247
229
248
subexit ( )
0 commit comments