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 8, 2023
1 parent 40f57f2 commit fa37377
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 33 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';

Check failure on line 1 in lib/from-markdown.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'vitest' or its corresponding type declarations.

Check failure on line 1 in lib/from-markdown.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'vitest' or its corresponding type declarations.
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';

Check failure on line 1 in lib/md2html.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'vitest' or its corresponding type declarations.

Check failure on line 1 in lib/md2html.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'vitest' or its corresponding type declarations.

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';

Check failure on line 1 in lib/md2md.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'vitest' or its corresponding type declarations.

Check failure on line 1 in lib/md2md.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'vitest' or its corresponding type declarations.

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';

Check failure on line 1 in lib/to-hast.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'vitest' or its corresponding type declarations.

Check failure on line 1 in lib/to-hast.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'vitest' or its corresponding type declarations.

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';

Check failure on line 2 in lib/to-hast.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'hast' or its corresponding type declarations.

Check failure on line 2 in lib/to-hast.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'hast' or its corresponding type declarations.
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';

Check failure on line 1 in lib/to-markdown.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'vitest' or its corresponding type declarations.

Check failure on line 1 in lib/to-markdown.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'vitest' or its corresponding type declarations.

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;
}
}

0 comments on commit fa37377

Please sign in to comment.