Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support latest version of micromark & mdx #10

Merged
merged 5 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support latest version of micromark & mdx
  • Loading branch information
Eyas committed Jun 22, 2024
commit ead258f43963575fd59107ab2abbff7607feac4f
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages/**/*.d.ts
!packages/mdast-heading-id/tree-extension.d.ts
!packages/**/ambient.d.ts

# Logs
logs
Expand Down
7,368 changes: 3,807 additions & 3,561 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
"bugs": "https://github.com/Eyas/md-heading-id/issues",
"repository": "github:Eyas/md-heading-id",
"devDependencies": {
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"cross-env": "^7.0.3",
"eslint": "^8.37.0",
"eslint-config-prettier": "^8.8.0",
"jest": "^29.5.0",
"prettier": "^2.8.7",
"rimraf": "^4.4.1",
"typescript": "^5.0.3"
"eslint": "^9.5.0",
"eslint-config-prettier": "^9.1.0",
"jest": "^29.7.0",
"prettier": "^3.3.2",
"prettier-2": "npm:prettier@^2",
"rimraf": "^5.0.7",
"typescript": "^5.5.2"
},
"scripts": {
"clean": "npm run clean --workspaces",
Expand Down
3 changes: 3 additions & 0 deletions packages/mdast-heading-id/__tests__/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {fromMarkdown} from 'mdast-util-from-markdown';
import {micromarkHeadingId} from 'micromark-heading-id';
import {mdastHeadingId} from '../index.js';

import {jest} from '@jest/globals';
jest.useFakeTimers();

describe('mdast plugin', () => {
it('emits id node', () => {
expect(
Expand Down
12 changes: 10 additions & 2 deletions packages/mdast-heading-id/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ export function mdastHeadingId() {
exit: {
idString(token) {
const idString = this.resume();
const node = /** @type {MdIdString} */ (this.exit(token));
node.value = idString;
const node = this.stack.length >= 1 ? this.stack[this.stack.length - 1] : undefined;
this.exit(token);
if (!node) {
return;
}
if (node.type !== 'idString') {
console.error(node);
return;
}
(/** @type {MdIdString} */ node).value = idString;
},
},
};
Expand Down
8 changes: 4 additions & 4 deletions packages/mdast-heading-id/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mdast-heading-id",
"version": "1.0.1",
"version": "2.0.0",
"description": "mdast plugin to support Markdown Extension for custom heading IDs",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -28,9 +28,9 @@
"tree-extension.d.ts"
],
"devDependencies": {
"@types/mdast": "^3.0.11",
"@types/unist": "^2.0.6",
"mdast-util-from-markdown": "^1.3.0",
"@types/mdast": "^4.0.4",
"@types/unist": "^3.0.2",
"mdast-util-from-markdown": "^2.0.1",
"micromark-heading-id": "*"
},
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions packages/mdast-heading-id/tree-extension.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ declare module 'mdast' {
interface StaticPhrasingContentMap {
idString: MdIdString
}
interface RootContentMap {
idString: MdIdString
}
interface PhrasingContentMap {
idString: MdIdString
}
}
3 changes: 3 additions & 0 deletions packages/micromark-heading-id/__tests__/plain-micromark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {micromark} from 'micromark';

import {micromarkHeadingId} from '../index.js';

import {jest} from '@jest/globals';
jest.useFakeTimers();

/** @type {import('micromark-util-types').HtmlExtension} */
const inspectSyntaxHtmlExtension = {
enter: {
Expand Down
9 changes: 9 additions & 0 deletions packages/micromark-heading-id/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {} from 'micromark-util-types'

declare module 'micromark-util-types' {
interface TokenTypeMap {
id: 'id'
idMarker: 'idMarker'
idString: 'idString'
}
}
6 changes: 4 additions & 2 deletions packages/micromark-heading-id/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @typedef {import("micromark-util-types").Extension} Extension
*/

import {codes} from 'micromark-util-symbol/codes.js';
import {codes} from 'micromark-util-symbol';

/**
* @returns {Extension} Fully-configured micromark extension for adding hading IDs
Expand Down Expand Up @@ -55,7 +55,9 @@ export function micromarkHeadingId() {
null,
codes.backslash,
codes.leftCurlyBrace,
].includes(code)
].includes(
// @ts-ignore
code)
) {
return nok(code);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/micromark-heading-id/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createRequire } from 'node:module';

/** @type {import('jest').Config} */
export const config = {
prettierPath: createRequire(import.meta.url).resolve('prettier-2'),
testRegex: "/__tests__/.*\\.js$",
};

export default config;
13 changes: 5 additions & 8 deletions packages/micromark-heading-id/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micromark-heading-id",
"version": "1.0.1",
"version": "2.0.0",
"description": "micromark extension to support Markdown Extension for custom heading IDs",
"license": "MIT",
"keywords": [
Expand All @@ -27,19 +27,16 @@
"types/index.d.ts"
],
"dependencies": {
"micromark-util-symbol": "^1.0.1"
"micromark-util-symbol": "^2.0.0"
},
"devDependencies": {
"micromark": "^3.1.0",
"micromark-util-types": "^1.0.2"
"micromark": "^4.0.0",
"micromark-util-types": "^2.0.0"
},
"scripts": {
"prepack": "npm run build",
"clean": "rimraf --glob __test__/**/*.d.ts *.d.ts",
"clean": "rimraf --glob __test__/**/*.d.ts index.d.ts",
"build": "npm run clean && tsc",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage"
},
"jest": {
"testRegex": "/__tests__/.*\\.js$"
}
}
4 changes: 2 additions & 2 deletions packages/micromark-heading-id/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["__tests__/**/*.js", "*.js"],
"compilerOptions": {"outDir": "types/"}
"include": ["__tests__/**/*.js", "*.js", "ambient.d.ts"],
"compilerOptions": {"outDir": "types/", "moduleResolution": "Bundler"}
}
3 changes: 3 additions & 0 deletions packages/remark-custom-heading-id/__tests__/just-remark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import html from 'remark-rehype';
import stringify from 'rehype-stringify';
import {remarkHeadingId} from '../index.js';

import {jest} from '@jest/globals';
jest.useFakeTimers();

describe('plugin with vanilla remark', function () {
it('should parse well', function () {
let contents = remark().use(remarkHeadingId).use(html).use(stringify)
Expand Down
39 changes: 25 additions & 14 deletions packages/remark-custom-heading-id/__tests__/remark-mdx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {remarkHeadingId} from '../index.js';
import {compile} from '@mdx-js/mdx';
import {VFile} from 'vfile';

import {jest} from '@jest/globals';
jest.useFakeTimers();

describe('plugin with @mdx-js/mdx', () => {
it('should parse well', async () => {
const file = await compile(
Expand All @@ -17,12 +20,13 @@ describe('plugin with @mdx-js/mdx', () => {
);

expect(String(file)).toMatchInlineSnapshot(`
"/*@jsxRuntime automatic @jsxImportSource react*/
""use strict";
const {Fragment: _Fragment, jsx: _jsx, jsxs: _jsxs} = arguments[0];
function _createMdxContent(props) {
const _components = Object.assign({
h1: "h1"
}, props.components);
const _components = {
h1: "h1",
...props.components
};
return _jsxs(_Fragment, {
children: [_jsx(_components.h1, {
children: "no id"
Expand All @@ -43,9 +47,12 @@ describe('plugin with @mdx-js/mdx', () => {
}
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {
children: _jsx(_createMdxContent, props)
})) : _createMdxContent(props);
return MDXLayout ? _jsx(MDXLayout, {
...props,
children: _jsx(_createMdxContent, {
...props
})
}) : _createMdxContent(props);
}
return {
default: MDXContent
Expand All @@ -66,12 +73,13 @@ describe('plugin with @mdx-js/mdx', () => {
);

expect(String(file)).toMatchInlineSnapshot(`
"/*@jsxRuntime automatic @jsxImportSource react*/
""use strict";
const {Fragment: _Fragment, jsx: _jsx, jsxs: _jsxs} = arguments[0];
function _createMdxContent(props) {
const _components = Object.assign({
h1: "h1"
}, props.components);
const _components = {
h1: "h1",
...props.components
};
return _jsxs(_Fragment, {
children: [_jsxs(_components.h1, {
children: ["no id ", 5 * 3]
Expand All @@ -83,9 +91,12 @@ describe('plugin with @mdx-js/mdx', () => {
}
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {
children: _jsx(_createMdxContent, props)
})) : _createMdxContent(props);
return MDXLayout ? _jsx(MDXLayout, {
...props,
children: _jsx(_createMdxContent, {
...props
})
}) : _createMdxContent(props);
}
return {
default: MDXContent
Expand Down
7 changes: 7 additions & 0 deletions packages/remark-custom-heading-id/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {} from 'mdast';

declare module 'mdast' {
interface HeadingData {
id?: string
}
}
13 changes: 2 additions & 11 deletions packages/remark-custom-heading-id/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@ import {mdastHeadingId} from 'mdast-heading-id';
export function remarkHeadingId() {
const data = this.data();

/**
* @param {string} key
* @param {unknown} value
*/
function add(key, value) {
const list = /** @type {unknown[]} */ (data[key]) || (data[key] = []);
list.push(value);
}

add('micromarkExtensions', micromarkHeadingId());
add('fromMarkdownExtensions', mdastHeadingId());
(data.micromarkExtensions || (data.micromarkExtensions = [])).push(micromarkHeadingId());
(data.fromMarkdownExtensions || (data.fromMarkdownExtensions = [])).push(mdastHeadingId());

return function (node) {
visit(node, 'idString', (node, _, parent) => {
Expand Down
9 changes: 9 additions & 0 deletions packages/remark-custom-heading-id/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createRequire } from 'node:module';

/** @type {import('jest').Config} */
export const config = {
prettierPath: createRequire(import.meta.url).resolve('prettier-2'),
testRegex: "/__tests__/.*\\.js$",
};

export default config;
23 changes: 10 additions & 13 deletions packages/remark-custom-heading-id/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remark-custom-heading-id",
"version": "1.0.1",
"version": "2.0.0",
"description": "remark plugin to support Markdown Extension for custom heading IDs",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -31,24 +31,21 @@
"dependencies": {
"mdast-heading-id": "*",
"micromark-heading-id": "*",
"unist-util-visit": "^4.1.2"
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@mdx-js/mdx": "^2.3.0",
"rehype-stringify": "^9.0.3",
"remark": "^14.0.2",
"remark-mdx": "^2.3.0",
"remark-rehype": "^10.1.0",
"unified": "^10.1.2",
"vfile": "^5.3.7"
"@mdx-js/mdx": "^3.0.1",
"rehype-stringify": "^10.0.0",
"remark": "^15.0.1",
"remark-mdx": "^3.0.1",
"remark-rehype": "^11.1.0",
"unified": "^11.0.5",
"vfile": "^6.0.1"
},
"scripts": {
"prepack": "npm run build",
"clean": "rimraf --glob __tests__/**/*.d.ts *.d.ts",
"clean": "rimraf --glob __tests__/**/*.d.ts index.d.ts",
"build": "npm run clean && tsc",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage"
},
"jest": {
"testRegex": "/__tests__/.*\\.js$"
}
}
2 changes: 1 addition & 1 deletion packages/remark-custom-heading-id/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["__tests__/**/*.js", "*.js", "../../jest.config.js"],
"include": ["__tests__/**/*.js", "*.js", "ambient.d.ts", "../../jest.config.js"],
"compilerOptions": {"outDir": "types/"}
}
Loading