How to render single post with theme ? #5418
Replies: 1 comment
-
'use strict';
const fs = require('fs-extra');
const Hexo = require('hexo');
const path = require('path');
const rootDir = path.join(__dirname, 'test-site');
fs.ensureDirSync(path.join(rootDir, 'tmp'));
const hexo = new Hexo(rootDir, { silent: false, debug: true, safe: true });
const main = async () => {
await hexo.init();
await hexo.load();
// Debugging: log resolved plugin path
const pluginPath = path.join(__dirname, '../dist/index.cjs');
console.log('Resolved plugin path:', pluginPath);
try {
const resolvedPluginPath = require.resolve(pluginPath);
console.log('Resolved full plugin path:', resolvedPluginPath);
if (!resolvedPluginPath) {
throw new Error('Plugin file not found or could not be resolved.');
}
await hexo.loadPlugin(resolvedPluginPath);
} catch (error) {
console.error('Error resolving or loading plugin:', error);
return;
}
const { compile } = Object.assign({}, hexo.extend.renderer.store.njk);
// Setup layout
const view = new hexo.theme.View('test.njk', ['<html>', '{{ content }}', '</html>'].join('\n'));
// Restore compile function
hexo.extend.renderer.store.njk.compile = compile;
// render post path
const postPath = path.join(__dirname, 'test-site/source/markdown-it/math.md');
const renderData = await hexo.post.render(postPath, {
content: fs.readFileSync(postPath, 'utf-8'),
engine: 'markdown'
});
// const body = ['layout: test', '---', '', content].join('\n');
const result = await view.render({
...renderData,
config: hexo.config,
page: {}
});
console.log(result);
await hexo.exit();
};
main(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
dimaslanjaka
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have script to render single post markdown.
how to render the post with theme and output the html result ?
@SukkaW @stevenjoezhang
Beta Was this translation helpful? Give feedback.
All reactions