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

[WIP] Next Major Release #39

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand Down
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions test/__snapshots__/remark-component.test.tsx.snap

This file was deleted.

Loading