From fa37377ba8a46c0af9d7b4dcf22c12381ea6bb6b Mon Sep 17 00:00:00 2001 From: Wataru Watanabe Date: Sun, 8 Oct 2023 11:57:49 +0900 Subject: [PATCH] chore!: update major version of mdast-utils --- jest.config.cjs | 10 ---------- lib/from-markdown.test.ts | 1 + lib/md2html.test.ts | 2 ++ lib/md2md.test.ts | 4 ++++ lib/to-hast.test.ts | 8 ++++++-- lib/to-hast.ts | 41 ++++++++++++++++++++++++++++++--------- lib/to-markdown.test.ts | 13 ++++++++----- lib/to-markdown.ts | 9 ++++++--- lib/types.ts | 16 +++++++++++---- 9 files changed, 71 insertions(+), 33 deletions(-) delete mode 100644 jest.config.cjs diff --git a/jest.config.cjs b/jest.config.cjs deleted file mode 100644 index b98a726..0000000 --- a/jest.config.cjs +++ /dev/null @@ -1,10 +0,0 @@ -/* - * For a detailed explanation regarding each configuration property, visit: - * https://jestjs.io/docs/configuration - */ - -module.exports = { - coverageProvider: 'v8', - - transform: {}, -}; diff --git a/lib/from-markdown.test.ts b/lib/from-markdown.test.ts index 0246fa0..dd21a08 100644 --- a/lib/from-markdown.test.ts +++ b/lib/from-markdown.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from 'vitest'; import { defListFromMarkdown } from './from-markdown'; import { fromMarkdown } from 'mdast-util-from-markdown'; import { defList } from 'micromark-extension-definition-list'; diff --git a/lib/md2html.test.ts b/lib/md2html.test.ts index a56aca4..9707d67 100644 --- a/lib/md2html.test.ts +++ b/lib/md2html.test.ts @@ -1,3 +1,5 @@ +import { test, expect } from 'vitest'; + import { defListHastHandlers } from './to-hast'; import { defListFromMarkdown } from './from-markdown'; diff --git a/lib/md2md.test.ts b/lib/md2md.test.ts index a2add1d..b7282fe 100644 --- a/lib/md2md.test.ts +++ b/lib/md2md.test.ts @@ -1,3 +1,5 @@ +import { test, expect } from 'vitest'; + import { defListFromMarkdown } from './from-markdown'; import { defListToMarkdown } from './to-markdown'; @@ -14,6 +16,8 @@ const md2md = (md: string) => { }); return toMarkdown(mdast, { extensions: [defListToMarkdown], + fences: false, + listItemIndent: 'tab', }); }; diff --git a/lib/to-hast.test.ts b/lib/to-hast.test.ts index 17ef143..fe6cb9e 100644 --- a/lib/to-hast.test.ts +++ b/lib/to-hast.test.ts @@ -1,14 +1,18 @@ +import { test, expect } from 'vitest'; + import { toHast } from 'mdast-util-to-hast'; import { defListHastHandlers } from './to-hast'; -const mdast2hast = (mdast: any) => { +import type { Root } from 'mdast'; + +const mdast2hast = (mdast: Root) => { return toHast(mdast, { handlers: defListHastHandlers, }); }; test('mdast to hast', () => { - const mdast = { + const mdast: Root = { type: 'root', children: [ { diff --git a/lib/to-hast.ts b/lib/to-hast.ts index 2daf78f..9d78ce3 100644 --- a/lib/to-hast.ts +++ b/lib/to-hast.ts @@ -1,9 +1,15 @@ -import { all, Handler } from 'mdast-util-to-hast'; +import type { Handler } from 'mdast-util-to-hast'; +import type { Element } from 'hast'; import { u } from 'unist-builder'; import { types } from 'micromark-extension-definition-list'; +import type { + DefListNode as MdastDefList, + DefListTermNode as MdastDefListTerm, + DefListDescriptionNode as MdastDefListDescription, +} from './types.js'; -export const mdastDefList2hast: Handler = (h, node, _parent) => { - const items = all(h, node); +export const mdastDefList2hast: Handler = (state, node: MdastDefList, _parent) => { + const items = state.all(node); const children = []; for (let i = 0; i < items.length; i++) { children.push(u('text', '\n')); @@ -12,16 +18,31 @@ export const mdastDefList2hast: Handler = (h, node, _parent) => { if (items.length > 0) { children.push(u('text', '\n')); } - return h(node, 'dl', {}, children); + + const result: Element = { type: 'element', tagName: 'dl', children, properties: {} }; + state.patch(node, result); + return result; }; -export const mdastDefListTerm2hast: Handler = (h, node, _parent) => { - return h(node, 'dt', {}, all(h, node)); +export const mdastDefListTerm2hast: Handler = (state, node: MdastDefListTerm, _parent) => { + const result: Element = { + type: 'element', + tagName: 'dt', + properties: {}, + children: state.all(node), + }; + state.patch(node, result); + return result; }; -export const mdastDefListDescription2hast: Handler = (h, node, _parent) => { +export const mdastDefListDescription2hast: Handler = ( + state, + node: MdastDefListDescription, + _parent, +) => { const children = []; - const tmpChildren = all(h, node); + const tmpChildren = state.all(node); + for (let i = 0; i < tmpChildren.length; i++) { const child = tmpChildren[i]; @@ -40,7 +61,9 @@ export const mdastDefListDescription2hast: Handler = (h, node, _parent) => { children.push(u('text', '\n')); } - return h(node, 'dd', {}, children); + const result: Element = { type: 'element', tagName: 'dd', properties: {}, children }; + state.patch(node, result); + return result; }; export const defListHastHandlers = { diff --git a/lib/to-markdown.test.ts b/lib/to-markdown.test.ts index c6f5acd..f71b9e6 100644 --- a/lib/to-markdown.test.ts +++ b/lib/to-markdown.test.ts @@ -1,14 +1,17 @@ +import { test, expect } from 'vitest'; + import { defListToMarkdown } from './to-markdown'; import { toMarkdown } from 'mdast-util-to-markdown'; import { dedent } from 'ts-dedent'; +import type { Root } from 'mdast'; -const compile = (mdast: any) => +const compile = (mdast: Root) => toMarkdown(mdast, { extensions: [defListToMarkdown], }); test('mdast -> markdown: basic', () => { - const mdast = { + const mdast: Root = { type: 'root', children: [ { @@ -94,7 +97,7 @@ test('mdast -> markdown: basic', () => { }); test('mdast -> markdown: multiples items, spreading', () => { - const mdast = { + const mdast: Root = { type: 'root', children: [ { @@ -195,7 +198,7 @@ test('mdast -> markdown: multiples items, spreading', () => { }); test('mdast -> markdown: unsafe (1)', () => { - const mdast = { + const mdast: Root = { type: 'root', children: [ { @@ -280,7 +283,7 @@ test('mdast -> markdown: unsafe (1)', () => { }); test('mdast -> markdown: unsafe (2)', () => { - const mdast = { + const mdast: Root = { type: 'root', children: [ { diff --git a/lib/to-markdown.ts b/lib/to-markdown.ts index aecf237..e9072ea 100644 --- a/lib/to-markdown.ts +++ b/lib/to-markdown.ts @@ -1,4 +1,5 @@ import type { Handle, Join, Options } from 'mdast-util-to-markdown'; +import type { DefListNode, DefListDescriptionNode, DefListTermNode } from './types.js'; declare module 'mdast-util-to-markdown' { interface ConstructNameMap { @@ -8,23 +9,24 @@ declare module 'mdast-util-to-markdown' { } } -const defListHandler: Handle = (node, _parent, state, info) => { +const defListHandler: Handle = (node: DefListNode, _parent, state, info) => { const exit = state.enter('defList'); const value = state.containerFlow(node, info); exit(); return value; }; -const defListTermHandler: Handle = (node, _parent, state, info) => { +const defListTermHandler: Handle = (node: DefListTermNode, _parent, state, info) => { const exit = state.enter('defListTerm'); const subexit = state.enter('phrasing'); + // @ts-ignore -- cannot extend phrasingParents const value = state.containerPhrasing(node, { ...info, before: '\n', after: '\n' }); subexit(); exit(); return value; }; -const defListDescriptionHandler: Handle = (node, _parent, state, info) => { +const defListDescriptionHandler: Handle = (node: DefListDescriptionNode, _parent, state, info) => { const exit = state.enter('defListDescription'); const value = state.indentLines(state.containerFlow(node, info), map); exit(); @@ -43,6 +45,7 @@ const joinDefItems: Join = (left, right, parent, _state) => { if (parent.type !== 'defList') { return; } + // @ts-ignore - cannot extend FlowChildren type if (left.type === 'defListDescription' && right.type === 'defListTerm') { return 1; } diff --git a/lib/types.ts b/lib/types.ts index d072036..4d850be 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -22,12 +22,20 @@ export interface DefListDescriptionNode extends Parent { } declare module 'mdast' { - // module augmentation + interface RootContentMap { + deflist: DefListNode; + // mdast.RootContent contains all nodes even if they are not suppose to be children of Root. + deflistTerm: DefListTermNode; + deflistDescription: DefListDescriptionNode; + } + interface BlockContentMap { deflist: DefListNode; + deflistTerm: DefListTermNode; + deflistDescription: DefListDescriptionNode; + } - // HACK: these are NOT block contents - deflistterm: DefListTermNode; - deflistdescription: DefListDescriptionNode; + interface PhrasingContentMap { + deflistTerm: DefListTermNode; } }