|
| 1 | +import { copyFileSync, existsSync, mkdirSync } from "fs"; |
| 2 | +import { dirname, resolve } from "path"; |
| 3 | +import { readFileSync, writeFileSync } from "fs"; |
| 4 | + |
| 5 | +import postcss from "postcss"; |
| 6 | +import postcssImport from "postcss-import"; |
| 7 | +import tailwindcss from "tailwindcss"; |
| 8 | + |
| 9 | +// Helper function to process CSS files |
| 10 | +async function processCssFile(src, dest) { |
| 11 | + try { |
| 12 | + const css = readFileSync(src, "utf8"); |
| 13 | + |
| 14 | + // Process with PostCSS plugins, due to import statements and TailwindCSS |
| 15 | + const result = await postcss([postcssImport(), tailwindcss()]).process( |
| 16 | + css, |
| 17 | + { |
| 18 | + from: src, |
| 19 | + to: dest, |
| 20 | + } |
| 21 | + ); |
| 22 | + |
| 23 | + writeFileSync(dest, result.css); |
| 24 | + console.log(`✓ Processed markdown CSS: ${src} to ${dest}`); |
| 25 | + |
| 26 | + if (result.map) { |
| 27 | + writeFileSync(dest + ".map", result.map.toString()); |
| 28 | + } |
| 29 | + } catch (error) { |
| 30 | + console.error(`Error processing markdown CSS ${src}:`, error); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const createMarkdownPlugin = () => { |
| 35 | + return { |
| 36 | + name: "markdown-plugin", |
| 37 | + async buildStart() { |
| 38 | + // Copy markdown editor and related files |
| 39 | + const fileOperations = [ |
| 40 | + { |
| 41 | + type: "copy", |
| 42 | + src: resolve( |
| 43 | + process.cwd(), |
| 44 | + "vendor/arkecosystem/foundation/resources/assets/js/markdown-editor/markdown-editor.js" |
| 45 | + ), |
| 46 | + dest: resolve( |
| 47 | + process.cwd(), |
| 48 | + "resources/js/markdown-editor.js" |
| 49 | + ), |
| 50 | + }, |
| 51 | + { |
| 52 | + type: "copy", |
| 53 | + src: resolve( |
| 54 | + process.cwd(), |
| 55 | + "vendor/arkecosystem/foundation/resources/assets/js/markdown-editor/plugins/underline.js" |
| 56 | + ), |
| 57 | + dest: resolve( |
| 58 | + process.cwd(), |
| 59 | + "resources/js/plugins/underline.js" |
| 60 | + ), |
| 61 | + }, |
| 62 | + { |
| 63 | + type: "copy", |
| 64 | + src: resolve( |
| 65 | + process.cwd(), |
| 66 | + "vendor/arkecosystem/foundation/resources/assets/js/markdown-editor/utils/utils.js" |
| 67 | + ), |
| 68 | + dest: resolve(process.cwd(), "resources/js/utils/utils.js"), |
| 69 | + }, |
| 70 | + { |
| 71 | + type: "process-css", |
| 72 | + src: resolve( |
| 73 | + process.cwd(), |
| 74 | + "vendor/arkecosystem/foundation/resources/assets/css/markdown-editor.css" |
| 75 | + ), |
| 76 | + dest: resolve( |
| 77 | + process.cwd(), |
| 78 | + "resources/css/markdown-editor.css" |
| 79 | + ), |
| 80 | + }, |
| 81 | + ]; |
| 82 | + |
| 83 | + for (const operation of fileOperations) { |
| 84 | + const { src, dest, type } = operation; |
| 85 | + const destDir = dirname(dest); |
| 86 | + |
| 87 | + // Ensure destination directory exists |
| 88 | + if (!existsSync(destDir)) { |
| 89 | + mkdirSync(destDir, { recursive: true }); |
| 90 | + } |
| 91 | + |
| 92 | + if (existsSync(src)) { |
| 93 | + if (type === "copy") { |
| 94 | + copyFileSync(src, dest); |
| 95 | + console.log(`✓ Copied markdown JS: ${src} to ${dest}`); |
| 96 | + } else if (type === "process-css") { |
| 97 | + // Process CSS with PostCSS first |
| 98 | + await processCssFile(src, dest); |
| 99 | + } |
| 100 | + } else { |
| 101 | + console.warn(`⚠ Could not find markdown file: ${src}`); |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + |
| 106 | + configureServer(server) {}, |
| 107 | + |
| 108 | + buildEnd() {}, |
| 109 | + }; |
| 110 | +}; |
| 111 | + |
| 112 | +export default createMarkdownPlugin; |
0 commit comments