Skip to content

Commit c628864

Browse files
committed
Refactor to move implementation to lib/
1 parent 059e140 commit c628864

File tree

3 files changed

+93
-88
lines changed

3 files changed

+93
-88
lines changed

index.js

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,4 @@
1-
/**
2-
* @typedef {import('mdast').Delete} Delete
3-
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
4-
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
5-
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
6-
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
7-
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
8-
*/
9-
10-
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
11-
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
12-
13-
/** @type {FromMarkdownExtension} */
14-
export const gfmStrikethroughFromMarkdown = {
15-
canContainEols: ['delete'],
16-
enter: {strikethrough: enterStrikethrough},
17-
exit: {strikethrough: exitStrikethrough}
18-
}
19-
20-
/**
21-
* List of constructs that occur in phrasing (paragraphs, headings), but cannot
22-
* contain strikethroughs. So they sort of cancel each other out.
23-
*
24-
* Note: keep in sync with: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/c47743b/lib/unsafe.js#L11>
25-
*/
26-
const constructsWithoutStrikethrough = [
27-
'autolink',
28-
'destinationLiteral',
29-
'destinationRaw',
30-
'reference',
31-
'titleQuote',
32-
'titleApostrophe'
33-
]
34-
35-
/** @type {ToMarkdownExtension} */
36-
export const gfmStrikethroughToMarkdown = {
37-
unsafe: [
38-
{
39-
character: '~',
40-
inConstruct: 'phrasing',
41-
// @ts-expect-error: register.
42-
notInConstruct: constructsWithoutStrikethrough
43-
}
44-
],
45-
handlers: {delete: handleDelete}
46-
}
47-
48-
handleDelete.peek = peekDelete
49-
50-
/**
51-
* @this {CompileContext}
52-
* @type {FromMarkdownHandle}
53-
*/
54-
function enterStrikethrough(token) {
55-
this.enter({type: 'delete', children: []}, token)
56-
}
57-
58-
/**
59-
* @this {CompileContext}
60-
* @type {FromMarkdownHandle}
61-
*/
62-
function exitStrikethrough(token) {
63-
this.exit(token)
64-
}
65-
66-
/**
67-
* @type {ToMarkdownHandle}
68-
* @param {Delete} node
69-
*/
70-
function handleDelete(node, _, context, safeOptions) {
71-
const tracker = track(safeOptions)
72-
// To do: next major (?): use `delete` or `strikethrough`?
73-
const exit = context.enter('emphasis')
74-
let value = tracker.move('~~')
75-
value += containerPhrasing(node, context, {
76-
...tracker.current(),
77-
before: value,
78-
after: '~'
79-
})
80-
value += tracker.move('~~')
81-
exit()
82-
return value
83-
}
84-
85-
/** @type {ToMarkdownHandle} */
86-
function peekDelete() {
87-
return '~'
88-
}
1+
export {
2+
gfmStrikethroughFromMarkdown,
3+
gfmStrikethroughToMarkdown
4+
} from './lib/index.js'

lib/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @typedef {import('mdast').Delete} Delete
3+
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
4+
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
5+
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
6+
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
7+
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
8+
*/
9+
10+
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
11+
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
12+
13+
/** @type {FromMarkdownExtension} */
14+
export const gfmStrikethroughFromMarkdown = {
15+
canContainEols: ['delete'],
16+
enter: {strikethrough: enterStrikethrough},
17+
exit: {strikethrough: exitStrikethrough}
18+
}
19+
20+
/**
21+
* List of constructs that occur in phrasing (paragraphs, headings), but cannot
22+
* contain strikethroughs. So they sort of cancel each other out.
23+
*
24+
* Note: keep in sync with: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/c47743b/lib/unsafe.js#L11>
25+
*/
26+
const constructsWithoutStrikethrough = [
27+
'autolink',
28+
'destinationLiteral',
29+
'destinationRaw',
30+
'reference',
31+
'titleQuote',
32+
'titleApostrophe'
33+
]
34+
35+
/** @type {ToMarkdownExtension} */
36+
export const gfmStrikethroughToMarkdown = {
37+
unsafe: [
38+
{
39+
character: '~',
40+
inConstruct: 'phrasing',
41+
// @ts-expect-error: register.
42+
notInConstruct: constructsWithoutStrikethrough
43+
}
44+
],
45+
handlers: {delete: handleDelete}
46+
}
47+
48+
handleDelete.peek = peekDelete
49+
50+
/**
51+
* @this {CompileContext}
52+
* @type {FromMarkdownHandle}
53+
*/
54+
function enterStrikethrough(token) {
55+
this.enter({type: 'delete', children: []}, token)
56+
}
57+
58+
/**
59+
* @this {CompileContext}
60+
* @type {FromMarkdownHandle}
61+
*/
62+
function exitStrikethrough(token) {
63+
this.exit(token)
64+
}
65+
66+
/**
67+
* @type {ToMarkdownHandle}
68+
* @param {Delete} node
69+
*/
70+
function handleDelete(node, _, context, safeOptions) {
71+
const tracker = track(safeOptions)
72+
// To do: next major (?): use `delete` or `strikethrough`?
73+
const exit = context.enter('emphasis')
74+
let value = tracker.move('~~')
75+
value += containerPhrasing(node, context, {
76+
...tracker.current(),
77+
before: value,
78+
after: '~'
79+
})
80+
value += tracker.move('~~')
81+
exit()
82+
return value
83+
}
84+
85+
/** @type {ToMarkdownHandle} */
86+
function peekDelete() {
87+
return '~'
88+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"main": "index.js",
3535
"types": "index.d.ts",
3636
"files": [
37+
"lib/",
3738
"index.d.ts",
3839
"index.js"
3940
],

0 commit comments

Comments
 (0)