From 6733670215c099f981eb85a3dd7c2b2f9c526e98 Mon Sep 17 00:00:00 2001 From: Orion Reed Date: Sun, 7 Jul 2024 23:55:38 +0100 Subject: [PATCH] try again --- build/markdownPlugin.js | 17 ++++++++++++++++ build/markdownToHtml.js | 39 ++++++++++++++++++++++++++++++++++++ src/utils/markdownPlugin.ts | 18 ----------------- src/utils/markdownToHtml.ts | 40 ------------------------------------- tsconfig.node.json | 2 +- vite.config.ts | 2 +- 6 files changed, 58 insertions(+), 60 deletions(-) create mode 100644 build/markdownPlugin.js create mode 100644 build/markdownToHtml.js delete mode 100644 src/utils/markdownPlugin.ts delete mode 100644 src/utils/markdownToHtml.ts diff --git a/build/markdownPlugin.js b/build/markdownPlugin.js new file mode 100644 index 0000000..3fa79ef --- /dev/null +++ b/build/markdownPlugin.js @@ -0,0 +1,17 @@ +import matter from "gray-matter"; +import { markdownToHtml } from "./markdownToHtml"; +import path from "path"; + +export const markdownPlugin = { + name: "markdown-plugin", + enforce: "pre", + transform(code, id) { + if (id.endsWith(".md")) { + const { data, content } = matter(code); + const filename = path.basename(id, ".md"); + const html = markdownToHtml(filename, content); + const htmlString = JSON.stringify({ html }); + return `export default ${htmlString};`; + } + }, +}; diff --git a/build/markdownToHtml.js b/build/markdownToHtml.js new file mode 100644 index 0000000..5ffd8f5 --- /dev/null +++ b/build/markdownToHtml.js @@ -0,0 +1,39 @@ +import MarkdownIt from "markdown-it"; + +const md = new MarkdownIt({ + html: true, + breaks: true, + linkify: true, +}); + +const mediaSrc = (folderName, fileName) => { + return `/posts/${folderName}/${fileName}`; +}; + +// Customize Markdown-to-HTML mapping here +// md.renderer.rules.paragraph_open = () => '

'; +md.renderer.rules.code_block = (tokens, idx, options, env, self) => { + console.log("tokens", tokens); + return `${tokens[idx].content}`; +}; +md.renderer.rules.image = (tokens, idx, options, env, self) => { + // console.log('env', env) + const token = tokens[idx]; + const src = token.attrGet("src"); + const alt = token.content; + const postName = env.postName; + const formattedSrc = mediaSrc(postName, src); + + if (src.endsWith(".mp4")) { + return ``; + } + + return `${alt}`; +}; + +export function markdownToHtml(postName, content) { + return md.render(content, { postName: postName }); +} diff --git a/src/utils/markdownPlugin.ts b/src/utils/markdownPlugin.ts deleted file mode 100644 index 17d45c1..0000000 --- a/src/utils/markdownPlugin.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Plugin } from 'vite' -import matter from 'gray-matter'; -import { markdownToHtml } from './markdownToHtml'; -import path from 'path'; - -export const markdownPlugin: Plugin = { - name: 'markdown-plugin', - enforce: 'pre', - transform(code, id) { - if (id.endsWith('.md')) { - const { data, content } = matter(code); - const filename = path.basename(id, '.md'); - const html = markdownToHtml(filename, content); - const htmlString = JSON.stringify({ html }); - return `export default ${htmlString};`; - } - }, -}; \ No newline at end of file diff --git a/src/utils/markdownToHtml.ts b/src/utils/markdownToHtml.ts deleted file mode 100644 index 6396d85..0000000 --- a/src/utils/markdownToHtml.ts +++ /dev/null @@ -1,40 +0,0 @@ -import MarkdownIt from 'markdown-it'; - -const md = new MarkdownIt({ - html: true, - breaks: true, - linkify: true, -}); - -const mediaSrc = (folderName: string, fileName: string) => { - return `/posts/${folderName}/${fileName}` -} - -// Customize Markdown-to-HTML mapping here -// md.renderer.rules.paragraph_open = () => '

'; -md.renderer.rules.code_block = (tokens, idx, options, env, self) => { - console.log('tokens', tokens) - return `${tokens[idx].content}`; -} -md.renderer.rules.image = (tokens, idx, options, env, self) => { - // console.log('env', env) - const token = tokens[idx]; - const src = token.attrGet('src'); - const alt = token.content - const postName = env.postName - const formattedSrc = mediaSrc(postName, src) - - if (src.endsWith('.mp4')) { - return ``; - } - - return `${alt}`; - -}; - -export function markdownToHtml(postName: string, content: string): string { - return md.render(content, { postName: postName }); -} \ No newline at end of file diff --git a/tsconfig.node.json b/tsconfig.node.json index bf67dd4..eca6668 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -6,5 +6,5 @@ "moduleResolution": "bundler", "allowSyntheticDefaultImports": true }, - "include": ["vite.config.ts", "src/utils/*"] + "include": ["vite.config.ts"] } diff --git a/vite.config.ts b/vite.config.ts index 4f297d4..16e1695 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { markdownPlugin } from './src/utils/markdownPlugin'; +import { markdownPlugin } from './build/markdownPlugin'; import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import wasm from "vite-plugin-wasm";