From d83aa3ef3437d7f61a9da63619a08db18ffd4244 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 18 Sep 2023 13:07:04 +0200 Subject: [PATCH] Refactor code-style --- lib/index.js | 31 ++++++++++++------------------- package.json | 2 +- test/index.js | 20 ++++---------------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/lib/index.js b/lib/index.js index c7cf688..69e22e9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,6 @@ +/// +/// + /** * @typedef {import('mdast').Root} Root * @typedef {import('micromark-extension-frontmatter').Options} Options @@ -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} */ (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)) } diff --git a/package.json b/package.json index 278cd56..965e09d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.js b/test/index.js index b246a14..0c2f482 100644 --- a/test/index.js +++ b/test/index.js @@ -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) { @@ -25,21 +24,12 @@ 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`/) } ) @@ -47,7 +37,7 @@ test('remarkFrontmatter', async function (t) { 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`/) }) @@ -55,7 +45,7 @@ test('remarkFrontmatter', async function (t) { '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() @@ -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() @@ -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 {