Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 26, 2023
1 parent d783861 commit cc94bd9
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 108 deletions.
1 change: 0 additions & 1 deletion dev/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export {gfmStrikethroughHtml} from './lib/html.js'
export {gfmStrikethrough, type Options} from './lib/syntax.js'

declare module 'micromark-util-types' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface TokenTypeMap {
strikethroughSequence: 'strikethroughSequence'
strikethroughSequenceTemporary: 'strikethroughSequenceTemporary'
Expand Down
6 changes: 3 additions & 3 deletions dev/lib/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*
* @typedef Options
* Configuration (optional).
* @property {boolean} [singleTilde=true]
* Whether to support strikethrough with a single tilde.
* @property {boolean | null | undefined} [singleTilde=true]
* Whether to support strikethrough with a single tilde (default: `true`).
*
* Single tildes work on github.com, but are technically prohibited by the
* GFM spec.
Expand All @@ -27,7 +27,7 @@ import {types} from 'micromark-util-symbol/types.js'
/**
* Create an extension for `micromark` to enable GFM strikethrough syntax.
*
* @param {Options | null | undefined} [options]
* @param {Options | null | undefined} [options={}]
* Configuration.
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions`, to
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
"unicorn/prefer-node-protocol": "off"
},
"overrides": [
{
"files": "**/*.ts",
"rules": {
"@typescript-eslint/consistent-type-definitions": 0
}
},
{
"files": "test/**/*.js",
"rules": {
Expand Down
236 changes: 132 additions & 104 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,140 +4,168 @@ import test from 'node:test'
import {micromark} from 'micromark'
import {createGfmFixtures} from 'create-gfm-fixtures'
import {
gfmStrikethrough as syntax,
gfmStrikethroughHtml as html
gfmStrikethrough,
gfmStrikethroughHtml
} from 'micromark-extension-gfm-strikethrough'

test('core', async () => {
assert.deepEqual(
Object.keys(await import('micromark-extension-gfm-strikethrough')).sort(),
['gfmStrikethrough', 'gfmStrikethroughHtml'],
'should expose the public api'
)
})

test('markdown -> html (micromark)', () => {
const defaults = syntax()

assert.deepEqual(
micromark('a ~b~', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a <del>b</del></p>',
'should support strikethrough w/ one tilde'
)

assert.deepEqual(
micromark('a ~~b~~', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a <del>b</del></p>',
'should support strikethrough w/ two tildes'
)

assert.deepEqual(
micromark('a ~~~b~~~', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a ~~~b~~~</p>',
'should not support strikethrough w/ three tildes'
)

assert.deepEqual(
micromark('a \\~~~b~~ c', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a ~<del>b</del> c</p>',
'should support strikethrough w/ after an escaped tilde'
test('micromark-extension-gfm-strikethrough', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('micromark-extension-gfm-strikethrough')).sort(),
['gfmStrikethrough', 'gfmStrikethroughHtml']
)
})

await t.test('should support strikethrough w/ one tilde', async function () {
assert.deepEqual(
micromark('a ~b~', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a <del>b</del></p>'
)
})

await t.test('should support strikethrough w/ two tildes', async function () {
assert.deepEqual(
micromark('a ~~b~~', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a <del>b</del></p>'
)
})

await t.test(
'should not support strikethrough w/ three tildes',
async function () {
assert.deepEqual(
micromark('a ~~~b~~~', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a ~~~b~~~</p>'
)
}
)

assert.deepEqual(
micromark('a ~~b ~~c~~ d~~ e', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a <del>b <del>c</del> d</del> e</p>',
'should support nested strikethrough'
await t.test(
'should support strikethrough w/ after an escaped tilde',
async function () {
assert.deepEqual(
micromark('a \\~~~b~~ c', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a ~<del>b</del> c</p>'
)
}
)

assert.deepEqual(
micromark('a ~-1~ b', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a <del>-1</del> b</p>',
'should open if preceded by whitespace and followed by punctuation'
await t.test('should support nested strikethrough', async function () {
assert.deepEqual(
micromark('a ~~b ~~c~~ d~~ e', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a <del>b <del>c</del> d</del> e</p>'
)
})

await t.test(
'should open if preceded by whitespace and followed by punctuation',
async function () {
assert.deepEqual(
micromark('a ~-1~ b', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a <del>-1</del> b</p>'
)
}
)

assert.deepEqual(
micromark('a ~b.~ c', {
extensions: [defaults],
htmlExtensions: [html]
}),
'<p>a <del>b.</del> c</p>',
'should close if preceded by punctuation and followed by whitespace'
await t.test(
'should close if preceded by punctuation and followed by whitespace',
async function () {
assert.deepEqual(
micromark('a ~b.~ c', {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a <del>b.</del> c</p>'
)
}
)

assert.deepEqual(
micromark('~b.~.', {
extensions: [syntax({singleTilde: true})],
htmlExtensions: [html]
}),
'<p><del>b.</del>.</p>',
'should close if preceded and followed by punctuation (del)'
await t.test(
'should close if preceded and followed by punctuation (del)',
async function () {
assert.deepEqual(
micromark('~b.~.', {
extensions: [gfmStrikethrough({singleTilde: true})],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p><del>b.</del>.</p>'
)
}
)

assert.deepEqual(
micromark('a ~b~ ~~c~~ d', {
extensions: [syntax({singleTilde: false})],
htmlExtensions: [html]
}),
'<p>a ~b~ <del>c</del> d</p>',
'should not support strikethrough w/ one tilde if `singleTilde: false`'
await t.test(
'should not support strikethrough w/ one tilde if `singleTilde: false`',
async function () {
assert.deepEqual(
micromark('a ~b~ ~~c~~ d', {
extensions: [gfmStrikethrough({singleTilde: false})],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a ~b~ <del>c</del> d</p>'
)
}
)

assert.deepEqual(
micromark('a ~b~ ~~c~~ d', {
extensions: [syntax({singleTilde: true})],
htmlExtensions: [html]
}),
'<p>a <del>b</del> <del>c</del> d</p>',
'should support strikethrough w/ one tilde if `singleTilde: true`'
await t.test(
'should support strikethrough w/ one tilde if `singleTilde: true`',
async function () {
assert.deepEqual(
micromark('a ~b~ ~~c~~ d', {
extensions: [gfmStrikethrough({singleTilde: true})],
htmlExtensions: [gfmStrikethroughHtml]
}),
'<p>a <del>b</del> <del>c</del> d</p>'
)
}
)
})

test('fixtures', async () => {
test('fixtures', async function (t) {
const base = new URL('fixtures/', import.meta.url)

await createGfmFixtures(base, {rehypeStringify: {closeSelfClosing: true}})

const files = await fs.readdir(base)
let index = -1
const extname = '.md'

while (++index < files.length) {
const d = files[index]

for (const d of files) {
if (!d.endsWith(extname)) {
continue
}

const name = d.slice(0, -extname.length)
const input = await fs.readFile(new URL(d, base))
const expected = String(await fs.readFile(new URL(name + '.html', base)))
let actual = micromark(input, {
extensions: [syntax()],
htmlExtensions: [html]
})

if (actual && !/\n$/.test(actual)) {
actual += '\n'
}
await t.test(name, async function () {
const input = await fs.readFile(new URL(d, base))
const expected = String(await fs.readFile(new URL(name + '.html', base)))
let actual = micromark(input, {
extensions: [gfmStrikethrough()],
htmlExtensions: [gfmStrikethroughHtml]
})

assert.equal(actual, expected, name)
if (actual && !/\n$/.test(actual)) {
actual += '\n'
}

assert.equal(actual, expected)
})
}
})

0 comments on commit cc94bd9

Please sign in to comment.