Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 18, 2023
1 parent 35052b5 commit d83aa3e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
31 changes: 12 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference types="remark-parse" />
/// <reference types="remark-stringify" />

/**
* @typedef {import('mdast').Root} Root
* @typedef {import('micromark-extension-frontmatter').Options} Options
Expand Down Expand Up @@ -32,24 +35,14 @@ export default function remarkFrontmatter(options) {
const settings = options || emptyOptions
const data = self.data()

add('micromarkExtensions', frontmatter(settings))
add('fromMarkdownExtensions', frontmatterFromMarkdown(settings))
add('toMarkdownExtensions', frontmatterToMarkdown(settings))

/**
* @param {string} field
* @param {unknown} value
*/
function add(field, value) {
// Other extensions
// @ts-expect-error: to do: remove when remark is released.
let list = /** @type {Array<unknown>} */ (data[field])
if (!list) {
list = []
// @ts-expect-error: to do: remove when remark is released.
data[field] = list
}
const micromarkExtensions =
data.micromarkExtensions || (data.micromarkExtensions = [])
const fromMarkdownExtensions =
data.fromMarkdownExtensions || (data.fromMarkdownExtensions = [])
const toMarkdownExtensions =
data.toMarkdownExtensions || (data.toMarkdownExtensions = [])

list.push(value)
}
micromarkExtensions.push(frontmatter(settings))
fromMarkdownExtensions.push(frontmatterFromMarkdown(settings))
toMarkdownExtensions.push(frontmatterToMarkdown(settings))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"c8": "^8.0.0",
"is-hidden": "^2.0.0",
"prettier": "^3.0.0",
"remark": "^14.0.0",
"remark": "^15.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
Expand Down
20 changes: 4 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import process from 'node:process'
import test from 'node:test'
import {isHidden} from 'is-hidden'
import {remark} from 'remark'
import {unified} from 'unified'
import remarkFrontmatter from '../index.js'

test('remarkFrontmatter', async function (t) {
Expand All @@ -25,37 +24,28 @@ test('remarkFrontmatter', async function (t) {
})
})

await t.test(
'should not throw if without parser or compiler',
async function () {
assert.doesNotThrow(function () {
unified().use(remarkFrontmatter).freeze()
})
}
)

await t.test(
'should throw if not given a preset or a matter',
async function () {
assert.throws(function () {
// @ts-expect-error: check how invalid input is handled.
unified().use(remarkFrontmatter, [1]).freeze()
remark().use(remarkFrontmatter, [1]).freeze()
}, /^Error: Expected matter to be an object, not `1`/)
}
)

await t.test('should throw if given an unknown preset', async function () {
assert.throws(function () {
// @ts-expect-error: check how invalid input is handled.
unified().use(remarkFrontmatter, ['jsonml']).freeze()
remark().use(remarkFrontmatter, ['jsonml']).freeze()
}, /^Error: Missing matter definition for `jsonml`/)
})

await t.test(
'should throw if given a matter without `type`',
async function () {
assert.throws(function () {
unified()
remark()
// @ts-expect-error: check how invalid input is handled.
.use(remarkFrontmatter, [{marker: '*'}])
.freeze()
Expand All @@ -67,7 +57,7 @@ test('remarkFrontmatter', async function (t) {
'should throw if given a matter without `marker`',
async function () {
assert.throws(function () {
unified()
remark()
// @ts-expect-error: check how invalid input is handled.
.use(remarkFrontmatter, [{type: 'jsonml'}])
.freeze()
Expand Down Expand Up @@ -108,8 +98,6 @@ test('fixtures', async function (t) {
} catch {}

const proc = remark().use(remarkFrontmatter, config)
/** @type {Root} */
// @ts-expect-error: remove when remark is released.
const actual = proc.parse(input)

try {
Expand Down

0 comments on commit d83aa3e

Please # to comment.