Skip to content
Draft
Changes from all commits
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
56,510 changes: 23,096 additions & 33,414 deletions package-lock.json

Large diffs are not rendered by default.

49 changes: 31 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -10,23 +10,22 @@
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"type": "module",
"main": "dist/index.js",
"module": "dist/react-remark.esm.js",
"typings": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"build": "tsc && type-coverage",
"test": "node --loader ts-node/esm ./test/index.test.ts",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend to use lite https://github.com/privatenumber/tsx over ts-node, types linting can be a seperate task via tsc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, though neither will be needed in the end.

The goal is:

migrate TypeScript files to JavaScript with JSDoc based Typescript

"lint": "eslint .",
"prepare": "npm run build",
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable",
@@ -37,10 +36,10 @@
"react": ">=16.8"
},
"dependencies": {
"rehype-react": "^6.0.0",
"remark-parse": "^9.0.0",
"remark-rehype": "^8.0.0",
"unified": "^9.0.0"
"rehype-react": "^7.0.0",
"remark-parse": "^10.0.0",
"remark-rehype": "^9.0.0",
"unified": "^10.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
@@ -51,19 +50,33 @@
"@testing-library/react-hooks": "^7.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^4.0.0",
"eslint": "^7.0.0",
"eslint-config-prettier": "^8.0.0",
"eslint-plugin-prettier": "^3.0.0",
"husky": "^7.0.0",
"katex": "^0.13.0",
"pinst": "^2.0.0",
"prettier": "^2.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not v3?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this PR came before v3 was released 🙂
V3 would be good to include

"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-test-renderer": "^17.0.0",
"rehype-katex": "^5.0.0",
"rehype-raw": "^5.0.0",
"rehype-sanitize": "^4.0.0",
"remark-gfm": "^1.0.0",
"remark-math": "^4.0.0",
"tsdx": "^0.14.0",
"typescript": "^3.0.0"
"rehype-katex": "^6.0.0",
"rehype-raw": "^6.0.0",
"rehype-sanitize": "^5.0.0",
"remark-gfm": "^2.0.0",
"remark-math": "^5.0.0",
"ts-node": "^10.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"uvu": "^0.5.0"
},
"eslintConfig": {
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
]
},
"prettier": {
"printWidth": 80,
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -7,21 +7,17 @@ import {
useEffect,
useCallback,
} from 'react';
import unified, { PluggableList } from 'unified';
import remarkParse, { RemarkParseOptions } from 'remark-parse';
import { Options as RemarkRehypeOptions } from 'mdast-util-to-hast';
import remarkToRehype from 'remark-rehype';
import { unified, PluggableList } from 'unified';
import remarkParse, { Options as RemarkParseOptions } from 'remark-parse';
import remarkToRehype, { Options as RemarkRehypeOptions } from 'remark-rehype';
import rehypeReact, { Options as RehypeReactOptions } from 'rehype-react';

type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export interface UseRemarkSyncOptions {
remarkParseOptions?: RemarkParseOptions;
remarkToRehypeOptions?: RemarkRehypeOptions;
rehypeReactOptions?: PartialBy<
RehypeReactOptions<typeof createElement>,
'createElement'
>;
rehypeReactOptions?: PartialBy<RehypeReactOptions, 'createElement'>;
remarkPlugins?: PluggableList;
rehypePlugins?: PluggableList;
}
@@ -39,13 +35,13 @@ export const useRemarkSync = (
unified()
.use(remarkParse, remarkParseOptions)
.use(remarkPlugins)
.use(remarkToRehype, remarkToRehypeOptions)
.use(remarkToRehype, remarkToRehypeOptions ?? true)
.use(rehypePlugins)
.use(rehypeReact, {
createElement,
Fragment,
...rehypeReactOptions,
} as RehypeReactOptions<typeof createElement>)
} as RehypeReactOptions)
.processSync(source).result as ReactElement;

export interface UseRemarkOptions extends UseRemarkSyncOptions {
@@ -58,6 +54,7 @@ export const useRemark = ({
rehypeReactOptions,
remarkPlugins = [],
rehypePlugins = [],
// eslint-disable-next-line @typescript-eslint/no-empty-function
onError = () => {},
}: UseRemarkOptions = {}): [ReactElement | null, (source: string) => void] => {
const [reactContent, setReactContent] = useState<ReactElement | null>(null);
@@ -66,13 +63,13 @@ export const useRemark = ({
unified()
.use(remarkParse, remarkParseOptions)
.use(remarkPlugins)
.use(remarkToRehype, remarkToRehypeOptions)
.use(remarkToRehype, remarkToRehypeOptions ?? true)
.use(rehypePlugins)
.use(rehypeReact, {
createElement,
Fragment,
...rehypeReactOptions,
} as RehypeReactOptions<typeof createElement>)
} as RehypeReactOptions)
.process(source)
.then((vfile) => setReactContent(vfile.result as ReactElement))
.catch(onError);
7 changes: 0 additions & 7 deletions test/__snapshots__/remark-component.test.tsx.snap

This file was deleted.

463 changes: 0 additions & 463 deletions test/__snapshots__/remark-hook.test.ts.snap

This file was deleted.

2 changes: 2 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './remark-component.test.js';
import './remark-hook.test.js';
9 changes: 6 additions & 3 deletions test/remark-hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createElement } from 'react';
import type { ComponentPropsWithoutNode } from 'rehype-react';
import type { ComponentsWithoutNodeOptions } from 'rehype-react/lib/index';
import { renderHook, act } from '@testing-library/react-hooks';
import '@testing-library/jest-dom/extend-expect';
import remarkGfm from 'remark-gfm';
@@ -120,8 +120,11 @@ describe('useRemarkSync', () => {
useRemarkSync('# heading', {
rehypeReactOptions: {
components: {
h1: (props: ComponentPropsWithoutNode) =>
createElement('h2', props),
h1: (
props: Parameters<
ComponentsWithoutNodeOptions['components']['h1']
>
) => createElement('h2', props),
},
},
})
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -3,10 +3,12 @@
"compilerOptions": {
"module": "esnext",
"lib": ["dom", "esnext"],
"target": "es2016",
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,