We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
希望可以批量转换html写法的文件
The text was updated successfully, but these errors were encountered:
HTML Editor - LDDGO.NET
你已经安装了turndown和moment库。 你已经将targetFolderPath变量设置为包含HTML文件的目录的正确路径。 npm install turndown moment --save
下面是代码,支持直接html文件批量生成md文件,调用的库是turndown 和 moment 库 (moment 库用来解析html的正则和替换)
const fs = require('fs'); const path = require('path'); const TurndownService = require('turndown'); const moment = require('moment');
// 创建 Turndown 服务实例 const turndownService = new TurndownService();
// 设置HTML文件和Hexo _posts目录的路径 const htmlFilesPath = path.join(__dirname, 'html2md'); // 你的HTML文件目录 const hexoPostsPath = path.join(__dirname, 'source/_posts'); // Hexo的_posts目录
// 读取HTML文件目录中的所有文件 fs.readdir(htmlFilesPath, (err, files) => { if (err) { console.error('读取HTML文件目录失败', err); return; }
files.forEach(file => { if (path.extname(file) === '.html') { // 读取HTML文件内容 const htmlContent = fs.readFileSync(path.join(htmlFilesPath, file), 'utf8');
// 使用 Turndown 将HTML转换为Markdown const markdownContent = turndownService.turndown(htmlContent);
// 创建文章的Front-matter const postDate = moment(); // 设置日期,这里使用当前日期,你可以根据需要修改 const frontMatter = ` --- title: ${path.basename(file, '.html')} // 文章标题,根据需要修改 date: ${postDate.format('YYYY-MM-DD HH:mm:ss')} +0800 // 文章日期和时区 comments: false tag: "" main_color: "#9742ef" toc: true --- `;
// 创建完整的Markdown内容 const fullMarkdownContent = `${frontMatter}\n${markdownContent}`;
// 写入到Hexo的_posts目录 const postFileName = `${path.basename(file, '.html')}.md`; // 根据日期和文件名创建文件名 const postPath = path.join(hexoPostsPath, postFileName); fs.writeFileSync(postPath, fullMarkdownContent, 'utf8'); console.log(`文章已创建:${postPath}`); } }); });
hljs.highlightAll();
Sorry, something went wrong.
No branches or pull requests
Feature request(新功能建议)
希望可以批量转换html写法的文件
The text was updated successfully, but these errors were encountered: