mirror of
https://github.com/PR0M3TH3AN/Archivox.git
synced 2025-09-08 23:18:42 +00:00
Add plugin system
This commit is contained in:
@@ -44,6 +44,7 @@ function loadConfig(configPath = path.join(process.cwd(), 'config.yaml')) {
|
||||
darkMode: false
|
||||
},
|
||||
features: {},
|
||||
pluginsDir: 'plugins',
|
||||
plugins: []
|
||||
};
|
||||
|
||||
|
24
src/config/loadPlugins.js
Normal file
24
src/config/loadPlugins.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
function loadPlugins(config) {
|
||||
const dir = path.resolve(process.cwd(), config.pluginsDir || 'plugins');
|
||||
const names = Array.isArray(config.plugins) ? config.plugins : [];
|
||||
const plugins = [];
|
||||
for (const name of names) {
|
||||
const file = path.join(dir, name.endsWith('.js') ? name : `${name}.js`);
|
||||
if (fs.existsSync(file)) {
|
||||
try {
|
||||
const mod = require(file);
|
||||
plugins.push(mod);
|
||||
} catch (e) {
|
||||
console.error(`Failed to load plugin ${name}:`, e);
|
||||
}
|
||||
} else {
|
||||
console.warn(`Plugin not found: ${file}`);
|
||||
}
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
|
||||
module.exports = loadPlugins;
|
@@ -6,6 +6,7 @@ const Eleventy = require('@11ty/eleventy');
|
||||
const lunr = require('lunr');
|
||||
const { lexer } = require('marked');
|
||||
const loadConfig = require('../config/loadConfig');
|
||||
const loadPlugins = require('../config/loadPlugins');
|
||||
|
||||
async function readDirRecursive(dir) {
|
||||
const entries = await fs.promises.readdir(dir, { withFileTypes: true });
|
||||
@@ -55,6 +56,17 @@ function buildNav(pages) {
|
||||
|
||||
async function generate({ contentDir = 'content', outputDir = '_site', configPath } = {}) {
|
||||
const config = loadConfig(configPath);
|
||||
const plugins = loadPlugins(config);
|
||||
|
||||
async function runHook(name, data) {
|
||||
for (const plugin of plugins) {
|
||||
if (typeof plugin[name] === 'function') {
|
||||
const res = await plugin[name](data);
|
||||
if (res !== undefined) data = res;
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
if (!fs.existsSync(contentDir)) {
|
||||
console.error(`Content directory not found: ${contentDir}`);
|
||||
return;
|
||||
@@ -76,7 +88,9 @@ async function generate({ contentDir = 'content', outputDir = '_site', configPat
|
||||
continue; // skip unchanged
|
||||
}
|
||||
}
|
||||
const raw = await fs.promises.readFile(file, 'utf8');
|
||||
let raw = await fs.promises.readFile(file, 'utf8');
|
||||
const mdObj = await runHook('onParseMarkdown', { file: rel, content: raw });
|
||||
if (mdObj && mdObj.content) raw = mdObj.content;
|
||||
const parsed = matter(raw);
|
||||
const title = parsed.data.title || path.basename(rel, '.md');
|
||||
const tokens = lexer(parsed.content || '');
|
||||
@@ -124,6 +138,16 @@ async function generate({ contentDir = 'content', outputDir = '_site', configPat
|
||||
};
|
||||
await elev.write();
|
||||
|
||||
for (const page of pages) {
|
||||
const outPath = path.join(outputDir, page.file.replace(/\.md$/, '.html'));
|
||||
if (fs.existsSync(outPath)) {
|
||||
let html = await fs.promises.readFile(outPath, 'utf8');
|
||||
const result = await runHook('onPageRendered', { file: page.file, html });
|
||||
if (result && result.html) html = result.html;
|
||||
await fs.promises.writeFile(outPath, html);
|
||||
}
|
||||
}
|
||||
|
||||
for (const asset of assets) {
|
||||
const srcPath = path.join(contentDir, asset);
|
||||
const destPath = path.join(outputDir, asset);
|
||||
|
Reference in New Issue
Block a user