Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kovsu/vite-plugin-markdown-test
https://github.com/kovsu/vite-plugin-markdown-test
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kovsu/vite-plugin-markdown-test
- Owner: kovsu
- Created: 2022-10-12T12:43:29.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-12T13:52:48.000Z (about 2 years ago)
- Last Synced: 2024-04-17T06:55:28.695Z (7 months ago)
- Language: TypeScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Vite-plugin for markdown
### core code
```ts
const markdownRE = /<\/vite-markdown>/g;...
const matches = Array.from(code.matchAll(markdownRE));
...
try {
const data = fs
.readFileSync(resolve(dirname(id), match[1]), {
encoding: "utf-8",
})
.toString();
code = code.replaceAll(markdownRE, transformMarkdown(data));
} catch {
code = code.replaceAll(markdownRE, "");
}
```
First, get matching code.Then use **fs.readFileSync** get source file content, and use pkg [markdown-it](https://www.npmjs.com/package/markdown-it) to render it (make .md content to be like .html)
```ts
const transformMarkdown = (mdText: string): string => {
return `
${md.render(mdText)}
`;
};
```In the last, use function transformMarkdown's result to replace the component in vue.