Skip to content

Commit

Permalink
chore!: update major version of mdast-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wataru-chocola committed Oct 9, 2023
1 parent 40f57f2 commit 83f90f6
Show file tree
Hide file tree
Showing 11 changed files with 1,031 additions and 2,557 deletions.
10 changes: 0 additions & 10 deletions jest.config.cjs

This file was deleted.

1 change: 1 addition & 0 deletions lib/from-markdown.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 2 additions & 0 deletions lib/md2html.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { test, expect } from 'vitest';

import { defListHastHandlers } from './to-hast';
import { defListFromMarkdown } from './from-markdown';

Expand Down
4 changes: 4 additions & 0 deletions lib/md2md.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { test, expect } from 'vitest';

import { defListFromMarkdown } from './from-markdown';
import { defListToMarkdown } from './to-markdown';

Expand All @@ -14,6 +16,8 @@ const md2md = (md: string) => {
});
return toMarkdown(mdast, {
extensions: [defListToMarkdown],
fences: false,
listItemIndent: 'tab',
});
};

Expand Down
8 changes: 6 additions & 2 deletions lib/to-hast.test.ts
Original file line number Diff line number Diff line change
@@ -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: [
{
Expand Down
41 changes: 32 additions & 9 deletions lib/to-hast.ts
Original file line number Diff line number Diff line change
@@ -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'));
Expand All @@ -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];

Expand All @@ -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 = {
Expand Down
13 changes: 8 additions & 5 deletions lib/to-markdown.test.ts
Original file line number Diff line number Diff line change
@@ -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: [
{
Expand Down Expand Up @@ -94,7 +97,7 @@ test('mdast -> markdown: basic', () => {
});

test('mdast -> markdown: multiples items, spreading', () => {
const mdast = {
const mdast: Root = {
type: 'root',
children: [
{
Expand Down Expand Up @@ -195,7 +198,7 @@ test('mdast -> markdown: multiples items, spreading', () => {
});

test('mdast -> markdown: unsafe (1)', () => {
const mdast = {
const mdast: Root = {
type: 'root',
children: [
{
Expand Down Expand Up @@ -280,7 +283,7 @@ test('mdast -> markdown: unsafe (1)', () => {
});

test('mdast -> markdown: unsafe (2)', () => {
const mdast = {
const mdast: Root = {
type: 'root',
children: [
{
Expand Down
9 changes: 6 additions & 3 deletions lib/to-markdown.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
16 changes: 12 additions & 4 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@
"index.js"
],
"dependencies": {
"@types/mdast": "^3.0.10",
"@types/unist": "^2.0.8",
"mdast-util-from-markdown": "^1.0.0",
"mdast-util-to-hast": "^12.0.0",
"mdast-util-to-markdown": "^1.5.0",
"micromark-extension-definition-list": "^1.5.0",
"unist-builder": "^3.0.0"
"@types/mdast": "^4.0.1",
"@types/unist": "^3.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-to-hast": "^13.0.2",
"mdast-util-to-markdown": "^2.1.0",
"micromark-extension-definition-list": "^2.0.0",
"unist-builder": "^4.0.0"
},
"devDependencies": {
"@types/jest": "27.5.2",
"@types/hast": "^3.0.1",
"@typescript-eslint/eslint-plugin": "6.6.0",
"@typescript-eslint/parser": "6.6.0",
"eslint": "8.49.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.28.1",
"hast-util-to-html": "8.0.4",
"jest": "27.5.1",
"hast-util-to-html": "9.0.0",
"npm-check-updates": "16.10.12",
"prettier": "3.0.3",
"rimraf": "5.0.1",
"ts-dedent": "2.2.0",
"typescript": "5.1.3"
"typescript": "5.1.3",
"vitest": "^0.34.6"
},
"keywords": [
"mdast",
Expand All @@ -59,7 +59,8 @@
"lint": "eslint ./**/*.ts",
"lint-fix": "eslint --fix ./**/*.ts && prettier --write ./**/*.{ts,json}",
"typecheck": "tsc",
"test": "npm run build && NODE_OPTIONS=--experimental-vm-modules jest 'test\\.js' --testPathIgnorePatterns=\"/node_modules/\"",
"test": "vitest run .js",
"test:dev": "npm run clean && vitest",
"build": "npm run clean && tsc",
"prepack": "npm run build",
"clean": "rimraf index.js index.test.js lib/*.d.ts lib/*.js"
Expand Down
Loading

0 comments on commit 83f90f6

Please sign in to comment.