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 14, 2023
1 parent a9db0e3 commit 768e37e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
30 changes: 20 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('micromark-extension-frontmatter').Options} Options
* @typedef {import('unified').Processor<Root>} Processor
*/

import {frontmatter} from 'micromark-extension-frontmatter'
import {
frontmatterFromMarkdown,
frontmatterToMarkdown
} from 'mdast-util-frontmatter'
import {frontmatter} from 'micromark-extension-frontmatter'

/** @type {Options} */
const emptyOptions = 'yaml'

/**
* Plugin to add support for frontmatter.
* Add support for frontmatter.
*
* @this {import('unified').Processor}
* @type {import('unified').Plugin<[Options?]|void[], Root>}
* @param {Options | null | undefined} [options='yaml']
* Configuration (default: `'yaml'`).
* @returns {undefined}
* Nothing.
*/
export default function remarkFrontmatter(options = 'yaml') {
const data = this.data()
export default function remarkFrontmatter(options) {
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
const settings = options || emptyOptions
const data = self.data()

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

/**
* @param {string} field
Expand All @@ -29,7 +39,7 @@ export default function remarkFrontmatter(options = 'yaml') {
function add(field, value) {
// Other extensions
// @ts-expect-error: to do: remove when remark is released.
let list = /** @type {unknown[]} */ (data[field])
let list = /** @type {Array<unknown>} */ (data[field])
if (!list) {
list = []
// @ts-expect-error: to do: remove when remark is released.
Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ const file = await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ['yaml', 'toml'])
.use(() => (tree) => {
console.dir(tree)
.use(function () {
return (tree) {
console.dir(tree)
}
})
.process(await read('example.md'))

Expand Down
35 changes: 19 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@

import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import test from 'node:test'
import process from 'node:process'
import {unified} from 'unified'
import {remark} from 'remark'
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) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
'default'
])
})

await t.test('should not throw if not passed options', async function () {
assert.doesNotThrow(() => {
// @ts-expect-error: remove when remark is released.
assert.doesNotThrow(function () {
remark().use(remarkFrontmatter).freeze()
})
})

await t.test(
'should not throw if without parser or compiler',
async function () {
assert.doesNotThrow(() => {
assert.doesNotThrow(function () {
unified().use(remarkFrontmatter).freeze()
})
}
Expand All @@ -32,26 +37,26 @@ test('remarkFrontmatter', async function (t) {
await t.test(
'should throw if not given a preset or a matter',
async function () {
assert.throws(() => {
// @ts-expect-error: invalid input.
assert.throws(function () {
// @ts-expect-error: check how invalid input is handled.
unified().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(() => {
// @ts-expect-error: invalid input.
assert.throws(function () {
// @ts-expect-error: check how invalid input is handled.
unified().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(() => {
assert.throws(function () {
unified()
// @ts-expect-error: invalid input.
// @ts-expect-error: check how invalid input is handled.
.use(remarkFrontmatter, [{marker: '*'}])
.freeze()
}, /^Error: Missing `type` in matter `{"marker":"\*"}`/)
Expand All @@ -61,9 +66,9 @@ test('remarkFrontmatter', async function (t) {
await t.test(
'should throw if given a matter without `marker`',
async function () {
assert.throws(() => {
assert.throws(function () {
unified()
// @ts-expect-error: invalid input.
// @ts-expect-error: check how invalid input is handled.
.use(remarkFrontmatter, [{type: 'jsonml'}])
.freeze()
}, /^Error: Missing `marker` or `fence` in matter `{"type":"jsonml"}`/)
Expand Down Expand Up @@ -102,7 +107,6 @@ test('fixtures', async function (t) {
config = JSON.parse(String(await fs.readFile(configUrl)))
} catch {}

// @ts-expect-error: remove when remark is released.
const proc = remark().use(remarkFrontmatter, config)
/** @type {Root} */
// @ts-expect-error: remove when remark is released.
Expand All @@ -129,7 +133,6 @@ test('fixtures', async function (t) {

assert.deepEqual(actual, expected)

// @ts-expect-error: remove when remark is released.
assert.equal(String(await proc.process(input)), String(output))
})
}
Expand Down

0 comments on commit 768e37e

Please sign in to comment.