Skip to content

Commit

Permalink
Create use-markdown react hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewjordan committed Apr 8, 2024
1 parent 6d46c5d commit 17872a8
Show file tree
Hide file tree
Showing 17 changed files with 4,580 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# use-markdown
React hook for converting Markdown to HTML

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default {
// other rules...
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
project: ["./tsconfig.json", "./tsconfig.node.json"],
tsconfigRootDir: __dirname,
},
};
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
38 changes: 38 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { build } from "vite";
import pkg from "./package.json" assert { type: "json" };

(async () => {
await build({
build: {
outDir: `dist`,
sourcemap: true,
lib: {
name: "use-markdown",
entry: "./src/use-markdown.tsx",
fileName: "use-markdown",
},
minify: "esbuild",
rollupOptions: {
external: [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
],
output: {
globals: {
react: "React",
"react-dom": "ReactDOM",
"rehype-raw": "rehypeRaw",
"rehype-stringify": "rehypeStringify",
"remark-gfm": "remarkGfm",
"remark-parse": "remarkParse",
"remark-rehype": "remarkRehype",
unified: "unified",
},
exports: "named",
inlineDynamicImports: true,
},
treeshake: true,
},
},
});
})();
75 changes: 75 additions & 0 deletions fixtures/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
export const sample = `# Sample GitHub Flavored Markdown Document
## Introduction
This document is a demonstration of GitHub flavored Markdown which includes various elements like headers, lists, code blocks, and more.
## Basix Formatting
This is a **bold** text.
This is an *italic* text.
This is a \`code\` text.
This is a ~~strikethrough~~ text.
## Code Blocks
Syntax highlighting is a feature of GitHub flavored Markdown that allows you to add color to your code:
\`\`\`python
def hello_world():
print("Hello, world!")
\`\`\`
## Lists
### Unordered List
- Item 1
- Item 2
- Subitem 2.1
- Subitem 2.2
### Ordered List
1. First item
2. Second item
1. Subitem 2.1
2. Subitem 2.2
## Tables
| Syntax | Description |
| --------- | ----------- |
| Header | Title |
| Paragraph | Text |
## Footnotes
You can create footnotes like this[^1].
[^1]: This is a footnote.
## Task List
- [x] Task 1
- [ ] Task 2
- [ ] Task 3
## Links and Images
[Dog](https://en.wikipedia.org/wiki/Dog)
![Dog](https://upload.wikimedia.org/wikipedia/commons/a/af/Golden_retriever_eating_pigs_foot.jpg)
## Blockquotes
> Blockquotes can also be nested.
>> Like this.
## Auto Link Literals
http://www.example.com
`;
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@nulib/use-markdown</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/index.tsx"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { sample } from "./fixtures/markdown";
import useMarkdown from "./src/use-markdown";

const App = () => {
const { jsx } = useMarkdown(sample);
return jsx;
};

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Loading

0 comments on commit 17872a8

Please sign in to comment.