Skip to content

Commit e111719

Browse files
committed
Add support for passing footnote options
1 parent 5a2471c commit e111719

File tree

8 files changed

+92
-15
lines changed

8 files changed

+92
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
coverage/
66
node_modules/
77
yarn.lock
8+
!/index.d.ts

index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type {ToMarkdownOptions as FootnoteOptions} from 'mdast-util-gfm-footnote'
2+
import type {Options as TableOptions} from 'mdast-util-gfm-table'
3+
4+
/**
5+
* Configuration for `gfmToMarkdown` from `mdast-util-gfm`.
6+
*
7+
* Currently supports options for `mdast-util-gfm-footnote` and
8+
* `mdast-util-gfm-table`.
9+
*/
10+
export interface Options extends FootnoteOptions, TableOptions {}
11+
12+
export {gfmFromMarkdown, gfmToMarkdown} from './lib/index.js'

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
/**
2-
* @typedef {import('./lib/index.js').Options} Options
3-
*/
4-
1+
// Note: types exposed from `index.d.ts`.
52
export {gfmFromMarkdown, gfmToMarkdown} from './lib/index.js'

lib/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/**
22
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
3+
* @typedef {import('mdast-util-gfm').Options} Options
34
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
45
*/
56

6-
/**
7-
* @typedef {import('mdast-util-gfm-table').Options} Options
8-
* Configuration.
9-
*/
10-
117
import {
128
gfmAutolinkLiteralFromMarkdown,
139
gfmAutolinkLiteralToMarkdown
@@ -49,7 +45,7 @@ export function gfmFromMarkdown() {
4945
* literals, footnotes, strikethrough, tables, tasklists).
5046
*
5147
* @param {Options | null | undefined} [options]
52-
* Configuration.
48+
* Configuration (optional).
5349
* @returns {ToMarkdownExtension}
5450
* Extension for `mdast-util-to-markdown` to enable GFM (autolink literals,
5551
* footnotes, strikethrough, tables, tasklists).
@@ -58,7 +54,7 @@ export function gfmToMarkdown(options) {
5854
return {
5955
extensions: [
6056
gfmAutolinkLiteralToMarkdown(),
61-
gfmFootnoteToMarkdown(),
57+
gfmFootnoteToMarkdown(options),
6258
gfmStrikethroughToMarkdown(),
6359
gfmTableToMarkdown(options),
6460
gfmTaskListItemToMarkdown()

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,29 @@
9090
"version": "3.0.0",
9191
"xo": {
9292
"overrides": [
93+
{
94+
"files": [
95+
"**/*.d.ts"
96+
],
97+
"rules": {
98+
"@typescript-eslint/array-type": [
99+
"error",
100+
{
101+
"default": "generic"
102+
}
103+
],
104+
"@typescript-eslint/ban-types": [
105+
"error",
106+
{
107+
"extendDefaults": true
108+
}
109+
],
110+
"@typescript-eslint/consistent-type-definitions": [
111+
"error",
112+
"interface"
113+
]
114+
}
115+
},
93116
{
94117
"files": "test/**/*.js",
95118
"rules": {

readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,15 @@ Configuration (TypeScript type).
346346

347347
###### Fields
348348

349+
* `firstLineBlank` (`boolean`, default: `false`)
350+
— use a blank line for the first line of footnote definitions
351+
* `stringLength` (`((value: string) => number)`, default: `s => s.length`)
352+
— function to detect the length of table cell content, used when aligning
353+
the delimiters between cells
349354
* `tableCellPadding` (`boolean`, default: `true`)
350355
— whether to add a space of padding between delimiters and cells
351356
* `tablePipeAlign` (`boolean`, default: `true`)
352357
— whether to align the delimiters
353-
* `stringLength` (`((value: string) => number)`, default: `s => s.length`)
354-
— function to detect the length of table cell content, used when aligning
355-
the delimiters between cells
356358

357359
## HTML
358360

test/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,49 @@ test('markdown -> mdast', async function (t) {
9393
)
9494
}
9595
})
96+
97+
test('mdast -> markdown', async function (t) {
98+
await t.test('should support table options', async function () {
99+
assert.equal(
100+
toMarkdown(
101+
{
102+
type: 'table',
103+
children: [
104+
{
105+
type: 'tableRow',
106+
children: [
107+
{type: 'tableCell', children: [{type: 'text', value: 'aaa'}]},
108+
{type: 'tableCell', children: [{type: 'text', value: 'b'}]}
109+
]
110+
},
111+
{
112+
type: 'tableRow',
113+
children: [
114+
{type: 'tableCell', children: [{type: 'text', value: 'c'}]},
115+
{type: 'tableCell', children: [{type: 'text', value: 'ddd'}]}
116+
]
117+
}
118+
]
119+
},
120+
{extensions: [gfmToMarkdown({tablePipeAlign: false})]}
121+
),
122+
'| aaa | b |\n| - | - |\n| c | ddd |\n'
123+
)
124+
})
125+
126+
await t.test('should support footnote options', async function () {
127+
assert.equal(
128+
toMarkdown(
129+
{
130+
type: 'footnoteDefinition',
131+
identifier: 'a',
132+
children: [
133+
{type: 'paragraph', children: [{type: 'text', value: 'b'}]}
134+
]
135+
},
136+
{extensions: [gfmToMarkdown({firstLineBlank: true})]}
137+
),
138+
'[^a]:\n b\n'
139+
)
140+
})
141+
})

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2022"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js"]
14+
"include": ["**/*.js", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)