Repository: remarkjs/react-remark
Branch: main
Commit: 39553e5f5c9e
Files: 17
Total size: 35.8 KB
Directory structure:
gitextract_o57pck0s/
├── .github/
│ └── workflows/
│ ├── gh-page.yml
│ └── main.yml
├── .gitignore
├── .husky/
│ └── pre-commit
├── .storybook/
│ └── main.ts
├── LICENSE
├── package.json
├── readme.md
├── src/
│ └── index.ts
├── stories/
│ ├── remark-component.stories.tsx
│ ├── remark-hook-async.stories.tsx
│ └── remark-hook-sync.stories.tsx
├── test/
│ ├── __snapshots__/
│ │ ├── remark-component.test.tsx.snap
│ │ └── remark-hook.test.ts.snap
│ ├── remark-component.test.tsx
│ └── remark-hook.test.ts
└── tsconfig.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/workflows/gh-page.yml
================================================
name: Deploy to GitHub pages
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
persist-credentials: false
- name: Use Node
uses: actions/setup-node@v3
with:
node-version: 16
- name: Use cached node_modules
uses: actions/cache@v3
with:
path: node_modules
key: nodeModules-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
nodeModules-
- name: Install dependencies
run: npm ci
env:
CI: true
- name: Build storybook
run: npm run build-storybook
- name: Deploy to GitHub pages
uses: JamesIves/github-pages-deploy-action@releases/v3
with:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
BRANCH: gh-pages
FOLDER: storybook-static
CLEAN: true
SINGLE_COMMIT: true
================================================
FILE: .github/workflows/main.yml
================================================
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
strategy:
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest]
node-version: [12, 14, 16]
name: '${{ matrix.platform }}: node.js ${{ matrix.node-version }}'
runs-on: ${{ matrix.platform }}
steps:
- name: Begin CI...
uses: actions/checkout@v3
- name: Use Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Use cached node_modules
uses: actions/cache@v3
with:
path: node_modules
key: nodeModules-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
nodeModules-
- name: Install dependencies
run: npm ci
env:
CI: true
- name: Lint
if: ${{ matrix.platform != 'windows-latest' }}
run: npm run lint
env:
CI: true
- name: Test
run: npm test -- --ci --coverage --maxWorkers=2
env:
CI: true
- name: Build
run: npm run build
env:
CI: true
================================================
FILE: .gitignore
================================================
*.log
.DS_Store
node_modules
.cache
dist/
storybook-static/
coverage/
================================================
FILE: .husky/pre-commit
================================================
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm run lint
================================================
FILE: .storybook/main.ts
================================================
module.exports = {
stories: ["../stories/**/*"],
addons: ['@storybook/addon-essentials'],
};
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2020 Christian Murphy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: package.json
================================================
{
"version": "2.1.0",
"name": "react-remark",
"description": "Renders Markdown as React components",
"author": "Christian Murphy <christian.murphy.42@gmail.com>",
"license": "MIT",
"repository": "remarkjs/react-remark",
"bugs": "https://github.com/remarkjs/react-remark/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"main": "dist/index.js",
"module": "dist/react-remark.esm.js",
"typings": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"peerDependencies": {
"react": ">=16.8"
},
"dependencies": {
"rehype-react": "^6.0.0",
"remark-parse": "^9.0.0",
"remark-rehype": "^8.0.0",
"unified": "^9.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@storybook/addon-essentials": "^6.0.0",
"@storybook/react": "^6.0.0",
"@testing-library/jest-dom": "^5.0.0",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"husky": "^7.0.0",
"katex": "^0.13.0",
"pinst": "^2.0.0",
"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"
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"renovate": {
"extends": [
"config:base",
":preserveSemverRanges"
]
}
}
================================================
FILE: readme.md
================================================
# react-remark
[](https://github.com/remarkjs/react-remark/actions?query=workflow%3ACI)
[](https://www.npmjs.com/package/react-remark)
[](https://bundlephobia.com/result?p=react-remark)
**react-remark** offers a [React hook](https://reactjs.org/docs/hooks-intro.html) and [React component](https://reactjs.org/docs/glossary.html#components) based way of rendering [markdown](https://commonmark.org/) into [React](https://reactjs.org) using [remark](https://github.com/remarkjs/remark)
## Installation
_npm_
```
npm install --save react-remark
```
_yarn_
```
yarn add react-remark
```
## Usage
### As a hook
#### Render static content
```tsx
import React, { useEffect } from 'react';
import { useRemark } from 'react-remark';
const ExampleComponent = () => {
const [reactContent, setMarkdownSource] = useRemark();
useEffect(() => {
setMarkdownSource('# markdown header');
}, []);
return reactContent;
};
export default ExampleComponent;
```
#### Using input and events to update
```tsx
import React from 'react';
import { useRemark } from 'react-remark';
const ExampleComponent = () => {
const [reactContent, setMarkdownSource] = useRemark();
return (
<>
<input
type="text"
onChange={({ currentTarget }) => setMarkdownSource(currentTarget.value)}
/>
{reactContent}
</>
);
};
export default ExampleComponent;
```
### Server side rendering
```tsx
import React from 'react';
import { useRemarkSync } from 'react-remark';
const ExampleComponent = () => {
const reactContent = useRemarkSync('# markdown header');
return reactContent;
};
export default ExampleComponent;
```
:notebook: Note that some remark plugins are async, these plugins will error if used with `useRemarkSync`.
[More examples of usage as hook in storybook.](https://remarkjs.github.io/react-remark/?path=/story/remark-hook)
### As a component
#### Render static content
```tsx
import React, { useState } from 'react';
import { Remark } from 'react-remark';
const ExampleComponent = () => (
<Remark>{`
# header
1. ordered
2. list
`}</Remark>
);
export default ExampleComponent;
```
#### Using input and events to update
```tsx
import React, { useState } from 'react';
import { Remark } from 'react-remark';
const ExampleComponent = () => {
const [markdownSource, setMarkdownSource] = useState('');
return (
<>
<input
type="text"
onChange={({ currentTarget }) => setMarkdownSource(currentTarget.value)}
/>
<Remark>{markdownSource}</Remark>
</>
);
};
export default ExampleComponent;
```
[More examples of usage as component in storybook.](https://remarkjs.github.io/react-remark/?path=/story/remark-component)
## Examples
A set of runnable examples are provided through storybook at <https://remarkjs.github.io/react-remark>.
The source for the story files can be found in [_/stories_](./stories).
## Architecture
```
react-remark
+---------------------------------------------------------------------------------------------------------------------------------------------+
| |
| +----------+ +----------------+ +---------------+ +----------------+ +--------------+ |
| | | | | | | | | | | |
| -markdown->+ remark +-mdast->+ remark plugins +-mdast->+ remark-rehype +-hast->+ rehype plugins +-hast->+ rehype-react +-react elements-> |
| | | | | | | | | | | |
| +----------+ +----------------+ +---------------+ +----------------+ +--------------+ |
| |
+---------------------------------------------------------------------------------------------------------------------------------------------+
```
relevant links: [markdown](https://commonmark.org), [remark](https://github.com/remarkjs/remark), [mdast](https://github.com/syntax-tree/mdast), [remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md), [remark-rehype](https://github.com/remarkjs/remark-rehype), [hast](https://github.com/syntax-tree/hast), [rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md), [rehype-react](https://github.com/rehypejs/rehype-react)
## Options
- `remarkParseOptions` (Object) - configure how Markdown is parsed, same as [`remark-parse` options](https://github.com/remarkjs/remark/tree/main/packages/remark-parse#options)
- `remarkPlugins` (Array) - [remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) or [custom plugins](https://unifiedjs.com/learn/guide/create-a-plugin) to transform markdown content before it is translated to HTML (hast)
- `remarkToRehypeOptions` (Object) - configure how Markdown (mdast) is translated into HTML (hast), same as [`remark-rehype` options](https://github.com/remarkjs/remark-rehype#api)
- `rehypePlugins` (Array) - [rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) or [custom plugins](https://unifiedjs.com/learn/guide/create-a-plugin) to transform HTML (hast) before it is translated to React elements.
- `rehypeReactOptions` (Object) - configure how HTML (hast) is translated into React elements, same as [`rehype-react` options](https://github.com/rehypejs/rehype-react#options)
### Pass options to hook
```tsx
import React, { Fragment } from 'react';
import { useRemark } from 'react-remark';
import remarkGemoji from 'remark-gemoji';
import rehypeSlug from 'rehype-slug';
import rehypeAutoLinkHeadings from 'rehype-autolink-headings';
// ...
const [reactContent, setMarkdownSource] = useRemark({
remarkPlugins: [remarkGemoji],
remarkToRehypeOptions: { allowDangerousHtml: true },
rehypePlugins: [rehypeSlug, rehypeAutoLinkHeadings],
rehypeReactOptions: {
components: {
p: (props) => <p className="custom-paragraph" {...props} />,
},
},
});
```
### Pass options to component
```tsx
import React, { Fragment } from 'react';
import { Remark } from 'react-remark';
import remarkGemoji from 'remark-gemoji';
import rehypeSlug from 'rehype-slug';
import rehypeAutoLinkHeadings from 'rehype-autolink-headings';
// ...
<Remark
remarkPlugins={[remarkGemoji]}
remarkToRehypeOptions={{ allowDangerousHtml: true }}
rehypePlugins={[rehypeSlug, rehypeAutoLinkHeadings]}
rehypeReactOptions={{
components: {
p: (props) => <p className="custom-paragraph" {...props} />,
},
}}
>
{markdownSource}
</Remark>;
```
================================================
FILE: src/index.ts
================================================
import {
FunctionComponent,
Fragment,
ReactElement,
createElement,
useState,
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 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'
>;
remarkPlugins?: PluggableList;
rehypePlugins?: PluggableList;
}
export const useRemarkSync = (
source: string,
{
remarkParseOptions,
remarkToRehypeOptions,
rehypeReactOptions,
remarkPlugins = [],
rehypePlugins = [],
}: UseRemarkOptions = {}
): ReactElement =>
unified()
.use(remarkParse, remarkParseOptions)
.use(remarkPlugins)
.use(remarkToRehype, remarkToRehypeOptions)
.use(rehypePlugins)
.use(rehypeReact, {
createElement,
Fragment,
...rehypeReactOptions,
} as RehypeReactOptions<typeof createElement>)
.processSync(source).result as ReactElement;
export interface UseRemarkOptions extends UseRemarkSyncOptions {
onError?: (err: Error) => void;
}
export const useRemark = ({
remarkParseOptions,
remarkToRehypeOptions,
rehypeReactOptions,
remarkPlugins = [],
rehypePlugins = [],
onError = () => {},
}: UseRemarkOptions = {}): [ReactElement | null, (source: string) => void] => {
const [reactContent, setReactContent] = useState<ReactElement | null>(null);
const setMarkdownSource = useCallback((source: string) => {
unified()
.use(remarkParse, remarkParseOptions)
.use(remarkPlugins)
.use(remarkToRehype, remarkToRehypeOptions)
.use(rehypePlugins)
.use(rehypeReact, {
createElement,
Fragment,
...rehypeReactOptions,
} as RehypeReactOptions<typeof createElement>)
.process(source)
.then((vfile) => setReactContent(vfile.result as ReactElement))
.catch(onError);
}, []);
return [reactContent, setMarkdownSource];
};
export interface RemarkProps extends UseRemarkOptions {
children: string;
}
export const Remark: FunctionComponent<RemarkProps> = ({
children,
...useRemarkOptions
}: RemarkProps) => {
const [reactContent, setMarkdownSource] = useRemark(useRemarkOptions);
useEffect(() => {
setMarkdownSource(children);
}, [children, setMarkdownSource]);
return reactContent;
};
================================================
FILE: stories/remark-component.stories.tsx
================================================
import React from 'react';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import rehypeSanitize from 'rehype-sanitize';
import 'katex/dist/katex.min.css';
import { Remark } from '../src';
export default {
title: 'Remark Component',
component: Remark,
};
export const CommonMark = ({ content }) => <Remark>{content}</Remark>;
CommonMark.args = {
content: `# header
1. ordered
2. list
* unordered
* list`,
};
export const GithubFlavoredMarkdown = ({ content }) => (
<Remark remarkPlugins={[remarkGfm]}>{content}</Remark>
);
GithubFlavoredMarkdown.args = {
content: `# header
| column 1 | column 2 |
| -------- | -------- |
| first | row |
`,
};
export const MarkdownWithMath = ({ content }) => (
<Remark remarkPlugins={[remarkMath]} rehypePlugins={[rehypeKatex]}>
{content}
</Remark>
);
MarkdownWithMath.args = {
content: `Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
$$
L = \\frac{1}{2} \\rho v^2 S C_L
$$`,
};
export const MixedHTMLSanitized = ({ content }) => (
<Remark
remarkToRehypeOptions={{ allowDangerousHtml: true }}
rehypePlugins={[rehypeRaw, rehypeSanitize]}
>
{content}
</Remark>
);
MixedHTMLSanitized.args = {
content: `# header
<strong>mixed</strong>
<em>with</em>
<kbd>html</kbd>
`,
};
================================================
FILE: stories/remark-hook-async.stories.tsx
================================================
import { useEffect } from 'react';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import rehypeSanitize from 'rehype-sanitize';
import 'katex/dist/katex.min.css';
import { useRemarkSync } from '../src';
export default {
title: 'Remark Hooks/sync and ssr with useRemarkSync',
component: useRemarkSync,
};
export const CommonMark = ({ content }) => {
return useRemarkSync(content);
};
CommonMark.args = {
content: `# header
1. ordered
2. list
* unordered
* list`,
};
export const GithubFlavoredMarkdown = ({ content }) => {
return (
useRemarkSync(content, {
remarkPlugins: [remarkGfm],
}) || <></>
);
};
GithubFlavoredMarkdown.args = {
content: `# header
| column 1 | column 2 |
| -------- | -------- |
| first | row |
`,
};
export const MarkdownWithMath = ({ content }) => {
return useRemarkSync(content, {
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
});
};
MarkdownWithMath.args = {
content: `Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
$$
L = \\frac{1}{2} \\rho v^2 S C_L
$$`,
};
export const MixedHTMLSanitized = ({ content }) => {
return useRemarkSync(content, {
remarkToRehypeOptions: { allowDangerousHtml: true },
rehypePlugins: [rehypeRaw, rehypeSanitize],
});
};
MixedHTMLSanitized.args = {
content: `# header
<strong>mixed</strong>
<em>with</em>
<kbd>html</kbd>`,
};
================================================
FILE: stories/remark-hook-sync.stories.tsx
================================================
import { useEffect } from 'react';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import rehypeSanitize from 'rehype-sanitize';
import 'katex/dist/katex.min.css';
import { useRemark } from '../src';
export default {
title: 'Remark Hooks/standard use with useRemark',
component: useRemark,
};
export const CommonMark = ({ content }) => {
const [reactContent, setMarkdownSource] = useRemark();
useEffect(() => {
setMarkdownSource(content);
}, [content]);
return reactContent || <></>;
};
CommonMark.args = {
content: `# header
1. ordered
2. list
* unordered
* list`,
};
export const GithubFlavoredMarkdown = ({ content }) => {
const [reactContent, setMarkdownSource] = useRemark({
remarkPlugins: [remarkGfm],
});
useEffect(() => {
setMarkdownSource(content);
}, [content]);
return reactContent || <></>;
};
GithubFlavoredMarkdown.args = {
content: `# header
| column 1 | column 2 |
| -------- | -------- |
| first | row |
`,
};
export const MarkdownWithMath = ({ content }) => {
const [reactContent, setMarkdownSource] = useRemark({
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
});
useEffect(() => {
setMarkdownSource(content);
}, [content]);
return reactContent || <></>;
};
MarkdownWithMath.args = {
content: `Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.
$$
L = \\frac{1}{2} \\rho v^2 S C_L
$$`,
};
export const MixedHTMLSanitized = ({ content }) => {
const [reactContent, setMarkdownSource] = useRemark({
remarkToRehypeOptions: { allowDangerousHtml: true },
rehypePlugins: [rehypeRaw, rehypeSanitize],
});
useEffect(() => {
setMarkdownSource(content);
}, [content]);
return reactContent || <></>;
};
MixedHTMLSanitized.args = {
content: `# header
<strong>mixed</strong>
<em>with</em>
<kbd>html</kbd>`,
};
================================================
FILE: test/__snapshots__/remark-component.test.tsx.snap
================================================
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Remark should render content 1`] = `
<h1>
header
</h1>
`;
================================================
FILE: test/__snapshots__/remark-hook.test.ts.snap
================================================
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`useRemark should render content 1`] = `
<React.Fragment>
<h1>
header
</h1>
</React.Fragment>
`;
exports[`useRemark should support custom element renderer 1`] = `
<React.Fragment>
<h1>
heading
</h1>
</React.Fragment>
`;
exports[`useRemark should support gfm through remark plugins 1`] = `
<React.Fragment>
<p>
<a
href="https://example.com"
>
https://example.com
</a>
</p>
</React.Fragment>
`;
exports[`useRemark should support html through rehype plugins 1`] = `
<React.Fragment>
<p>
<span>
example
</span>
</p>
</React.Fragment>
`;
exports[`useRemark should support math through remark and rehype plugins 1`] = `
<React.Fragment>
<p>
Lift(
<span
className="math math-inline"
>
<span
className="katex"
>
<span
className="katex-mathml"
>
<math
xmlns="http://www.w3.org/1998/Math/MathML"
>
<semantics>
<mrow>
<mi>
L
</mi>
</mrow>
<annotation
encoding="application/x-tex"
>
L
</annotation>
</semantics>
</math>
</span>
<span
aria-hidden="true"
className="katex-html"
>
<span
className="base"
>
<span
className="strut"
style={
Object {
"height": "0.68333em",
"verticalAlign": "0em",
}
}
/>
<span
className="mord mathnormal"
>
L
</span>
</span>
</span>
</span>
</span>
) can be determined by Lift Coefficient (
<span
className="math math-inline"
>
<span
className="katex"
>
<span
className="katex-mathml"
>
<math
xmlns="http://www.w3.org/1998/Math/MathML"
>
<semantics>
<mrow>
<msub>
<mi>
C
</mi>
<mi>
L
</mi>
</msub>
</mrow>
<annotation
encoding="application/x-tex"
>
C_L
</annotation>
</semantics>
</math>
</span>
<span
aria-hidden="true"
className="katex-html"
>
<span
className="base"
>
<span
className="strut"
style={
Object {
"height": "0.83333em",
"verticalAlign": "-0.15em",
}
}
/>
<span
className="mord"
>
<span
className="mord mathnormal"
style={
Object {
"marginRight": "0.07153em",
}
}
>
C
</span>
<span
className="msupsub"
>
<span
className="vlist-t vlist-t2"
>
<span
className="vlist-r"
>
<span
className="vlist"
style={
Object {
"height": "0.32833099999999993em",
}
}
>
<span
style={
Object {
"marginLeft": "-0.07153em",
"marginRight": "0.05em",
"top": "-2.5500000000000003em",
}
}
>
<span
className="pstrut"
style={
Object {
"height": "2.7em",
}
}
/>
<span
className="sizing reset-size6 size3 mtight"
>
<span
className="mord mathnormal mtight"
>
L
</span>
</span>
</span>
</span>
<span
className="vlist-s"
>
</span>
</span>
<span
className="vlist-r"
>
<span
className="vlist"
style={
Object {
"height": "0.15em",
}
}
>
<span />
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
) like the following equation.
</p>
</React.Fragment>
`;
exports[`useRemarkSync should render content 1`] = `
<React.Fragment>
<h1>
header
</h1>
</React.Fragment>
`;
exports[`useRemarkSync should support custom element renderer 1`] = `
<React.Fragment>
<h1>
heading
</h1>
</React.Fragment>
`;
exports[`useRemarkSync should support gfm through remark plugins 1`] = `
<React.Fragment>
<p>
<a
href="https://example.com"
>
https://example.com
</a>
</p>
</React.Fragment>
`;
exports[`useRemarkSync should support html through rehype plugins 1`] = `
<React.Fragment>
<p>
<span>
example
</span>
</p>
</React.Fragment>
`;
exports[`useRemarkSync should support math through remark and rehype plugins 1`] = `
<React.Fragment>
<p>
Lift(
<span
className="math math-inline"
>
<span
className="katex"
>
<span
className="katex-mathml"
>
<math
xmlns="http://www.w3.org/1998/Math/MathML"
>
<semantics>
<mrow>
<mi>
L
</mi>
</mrow>
<annotation
encoding="application/x-tex"
>
L
</annotation>
</semantics>
</math>
</span>
<span
aria-hidden="true"
className="katex-html"
>
<span
className="base"
>
<span
className="strut"
style={
Object {
"height": "0.68333em",
"verticalAlign": "0em",
}
}
/>
<span
className="mord mathnormal"
>
L
</span>
</span>
</span>
</span>
</span>
) can be determined by Lift Coefficient (
<span
className="math math-inline"
>
<span
className="katex"
>
<span
className="katex-mathml"
>
<math
xmlns="http://www.w3.org/1998/Math/MathML"
>
<semantics>
<mrow>
<msub>
<mi>
C
</mi>
<mi>
L
</mi>
</msub>
</mrow>
<annotation
encoding="application/x-tex"
>
C_L
</annotation>
</semantics>
</math>
</span>
<span
aria-hidden="true"
className="katex-html"
>
<span
className="base"
>
<span
className="strut"
style={
Object {
"height": "0.83333em",
"verticalAlign": "-0.15em",
}
}
/>
<span
className="mord"
>
<span
className="mord mathnormal"
style={
Object {
"marginRight": "0.07153em",
}
}
>
C
</span>
<span
className="msupsub"
>
<span
className="vlist-t vlist-t2"
>
<span
className="vlist-r"
>
<span
className="vlist"
style={
Object {
"height": "0.32833099999999993em",
}
}
>
<span
style={
Object {
"marginLeft": "-0.07153em",
"marginRight": "0.05em",
"top": "-2.5500000000000003em",
}
}
>
<span
className="pstrut"
style={
Object {
"height": "2.7em",
}
}
/>
<span
className="sizing reset-size6 size3 mtight"
>
<span
className="mord mathnormal mtight"
>
L
</span>
</span>
</span>
</span>
<span
className="vlist-s"
>
</span>
</span>
<span
className="vlist-r"
>
<span
className="vlist"
style={
Object {
"height": "0.15em",
}
}
>
<span />
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
</span>
) like the following equation.
</p>
</React.Fragment>
`;
================================================
FILE: test/remark-component.test.tsx
================================================
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { Remark } from '../src';
describe('Remark', () => {
it('should render content', async () => {
const { container, getByText } = render(<Remark># header</Remark>);
await waitFor(() => {
expect(getByText('header')).toBeInTheDocument();
});
expect(container.firstChild).toMatchSnapshot();
});
});
================================================
FILE: test/remark-hook.test.ts
================================================
import { createElement } from 'react';
import type { ComponentPropsWithoutNode } from 'rehype-react';
import { renderHook, act } from '@testing-library/react-hooks';
import '@testing-library/jest-dom/extend-expect';
import remarkGfm from 'remark-gfm';
import rehypeRaw from 'rehype-raw';
import rehypeSanitize from 'rehype-sanitize';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import { useRemark, useRemarkSync } from '../src';
describe('useRemark', () => {
it('should render content', async () => {
const { result, waitForNextUpdate } = renderHook(() => useRemark());
act(() => {
result.current[1]('# header');
});
await waitForNextUpdate();
expect(result.current[0]).toMatchSnapshot();
});
it('should support gfm through remark plugins', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useRemark({ remarkPlugins: [remarkGfm] })
);
act(() => {
result.current[1]('https://example.com');
});
await waitForNextUpdate();
expect(result.current[0]).toMatchSnapshot();
});
it('should support html through rehype plugins', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useRemark({
remarkToRehypeOptions: { allowDangerousHtml: true },
rehypePlugins: [rehypeRaw, rehypeSanitize],
})
);
act(() => {
result.current[1]('<span>example</span>');
});
await waitForNextUpdate();
expect(result.current[0]).toMatchSnapshot();
});
it('should support math through remark and rehype plugins', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useRemark({
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
})
);
act(() => {
result.current[1](
'Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.'
);
});
await waitForNextUpdate();
expect(result.current[0]).toMatchSnapshot();
});
it('should support custom element renderer', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useRemark({
rehypeReactOptions: {
components: {
h1: (props: ComponentPropsWithoutNode) =>
createElement('h2', props),
},
},
})
);
act(() => {
result.current[1]('# heading');
});
await waitForNextUpdate();
expect(result.current[0]).toMatchSnapshot();
});
});
describe('useRemarkSync', () => {
it('should render content', async () => {
const { result } = renderHook(() => useRemarkSync('# header'));
expect(result.current).toMatchSnapshot();
});
it('should support gfm through remark plugins', async () => {
const { result } = renderHook(() =>
useRemarkSync('https://example.com', { remarkPlugins: [remarkGfm] })
);
expect(result.current).toMatchSnapshot();
});
it('should support html through rehype plugins', async () => {
const { result } = renderHook(() =>
useRemarkSync('<span>example</span>', {
remarkToRehypeOptions: { allowDangerousHtml: true },
rehypePlugins: [rehypeRaw, rehypeSanitize],
})
);
expect(result.current).toMatchSnapshot();
});
it('should support math through remark and rehype plugins', async () => {
const { result } = renderHook(() =>
useRemarkSync(
'Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following equation.',
{
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
}
)
);
expect(result.current).toMatchSnapshot();
});
it('should support custom element renderer', async () => {
const { result } = renderHook(() =>
useRemarkSync('# heading', {
rehypeReactOptions: {
components: {
h1: (props: ComponentPropsWithoutNode) =>
createElement('h2', props),
},
},
})
);
expect(result.current).toMatchSnapshot();
});
});
================================================
FILE: tsconfig.json
================================================
{
"include": ["src", "types"],
"compilerOptions": {
"module": "esnext",
"lib": ["dom", "esnext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"@": ["./"],
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
}
}
gitextract_o57pck0s/ ├── .github/ │ └── workflows/ │ ├── gh-page.yml │ └── main.yml ├── .gitignore ├── .husky/ │ └── pre-commit ├── .storybook/ │ └── main.ts ├── LICENSE ├── package.json ├── readme.md ├── src/ │ └── index.ts ├── stories/ │ ├── remark-component.stories.tsx │ ├── remark-hook-async.stories.tsx │ └── remark-hook-sync.stories.tsx ├── test/ │ ├── __snapshots__/ │ │ ├── remark-component.test.tsx.snap │ │ └── remark-hook.test.ts.snap │ ├── remark-component.test.tsx │ └── remark-hook.test.ts └── tsconfig.json
SYMBOL INDEX (4 symbols across 1 files)
FILE: src/index.ts
type PartialBy (line 16) | type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type UseRemarkSyncOptions (line 18) | interface UseRemarkSyncOptions {
type UseRemarkOptions (line 51) | interface UseRemarkOptions extends UseRemarkSyncOptions {
type RemarkProps (line 84) | interface RemarkProps extends UseRemarkOptions {
Condensed preview — 17 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (40K chars).
[
{
"path": ".github/workflows/gh-page.yml",
"chars": 1007,
"preview": "name: Deploy to GitHub pages\n\non:\n push:\n branches: [main]\n\njobs:\n build-and-deploy:\n runs-on: ubuntu-latest\n\n "
},
{
"path": ".github/workflows/main.yml",
"chars": 1170,
"preview": "name: CI\n\non:\n push:\n branches: [main]\n pull_request:\n branches: [main]\n\njobs:\n build:\n strategy:\n matr"
},
{
"path": ".gitignore",
"chars": 70,
"preview": "*.log\n.DS_Store\nnode_modules\n.cache\ndist/\nstorybook-static/\ncoverage/\n"
},
{
"path": ".husky/pre-commit",
"chars": 55,
"preview": "#!/bin/sh\n. \"$(dirname \"$0\")/_/husky.sh\"\n\nnpm run lint\n"
},
{
"path": ".storybook/main.ts",
"chars": 97,
"preview": "module.exports = {\n stories: [\"../stories/**/*\"],\n addons: ['@storybook/addon-essentials'],\n};\n"
},
{
"path": "LICENSE",
"chars": 1073,
"preview": "MIT License\n\nCopyright (c) 2020 Christian Murphy\n\nPermission is hereby granted, free of charge, to any person obtaining "
},
{
"path": "package.json",
"chars": 2035,
"preview": "{\n \"version\": \"2.1.0\",\n \"name\": \"react-remark\",\n \"description\": \"Renders Markdown as React components\",\n \"author\": \""
},
{
"path": "readme.md",
"chars": 7201,
"preview": "# react-remark\n\n[](https://github.com/"
},
{
"path": "src/index.ts",
"chars": 2730,
"preview": "import {\n FunctionComponent,\n Fragment,\n ReactElement,\n createElement,\n useState,\n useEffect,\n useCallback,\n} fro"
},
{
"path": "stories/remark-component.stories.tsx",
"chars": 1401,
"preview": "import React from 'react';\nimport remarkGfm from 'remark-gfm';\nimport remarkMath from 'remark-math';\nimport rehypeKatex "
},
{
"path": "stories/remark-hook-async.stories.tsx",
"chars": 1509,
"preview": "import { useEffect } from 'react';\nimport remarkGfm from 'remark-gfm';\nimport remarkMath from 'remark-math';\nimport rehy"
},
{
"path": "stories/remark-hook-sync.stories.tsx",
"chars": 1976,
"preview": "import { useEffect } from 'react';\nimport remarkGfm from 'remark-gfm';\nimport remarkMath from 'remark-math';\nimport rehy"
},
{
"path": "test/__snapshots__/remark-component.test.tsx.snap",
"chars": 113,
"preview": "// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`Remark should render content 1`] = `\n<h1>\n header\n</h1>\n`;\n"
},
{
"path": "test/__snapshots__/remark-hook.test.ts.snap",
"chars": 11173,
"preview": "// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`useRemark should render content 1`] = `\n<React.Fragment>\n <h1>\n "
},
{
"path": "test/remark-component.test.tsx",
"chars": 463,
"preview": "import React from 'react';\nimport { render, waitFor } from '@testing-library/react';\nimport '@testing-library/jest-dom/e"
},
{
"path": "test/remark-hook.test.ts",
"chars": 4045,
"preview": "import { createElement } from 'react';\nimport type { ComponentPropsWithoutNode } from 'rehype-react';\nimport { renderHoo"
},
{
"path": "tsconfig.json",
"chars": 547,
"preview": "{\n \"include\": [\"src\", \"types\"],\n \"compilerOptions\": {\n \"module\": \"esnext\",\n \"lib\": [\"dom\", \"esnext\"],\n \"impor"
}
]
About this extraction
This page contains the full source code of the remarkjs/react-remark GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 17 files (35.8 KB), approximately 9.2k tokens, and a symbol index with 4 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.