Node.js批量转换Markdown为JSON时输出标准数组格式问题
问题背景
本人具备C++开发基础,目前正在学习JavaScript,正在开发一款实用工具,可接收.md(Markdown)文件,将其解析转换为HTML后生成JSON对象,最终按照指定格式写入已存在的JSON文件中。
工具的完整处理流水线如下:
- 遍历Markdown文件目录 -> 逐文件执行解析转换,将Markdown内容转为HTML -> 将HTML内容与解析得到的元数据写入JSON文件
说明:目前大部分逻辑已开发完成,仅存在JSON格式相关问题需要协助,请完整阅读问题内容后再作答,感谢!
现有实现代码
流水线核心逻辑
export function transformMdtoHtmltoJson(dest, post) { const file = matter.read(post); const title = file.data.title; const date = file.data.date; const description = file.data.description; const socials = file.data.socials; unified() .use(remarkParse) .use(remarkRehype) .use(rehypeStringify) .process(file.content) .then( (result) => { toJson(dest, title, date, description, result.value, socials); }, (err) => { throw err; } ); }
JSON文件写入模块
class Data { constructor(title, date, description, content, socials) { this.title = title; this.date = date; this.description = description; this.content = content; this.socials = socials; } } function createNewMember(title, date, description, content, socials) { let newMemer = new Data(title, date, description, content, socials); let result = {}; return result = { "title": newMemer.title, "date": newMemer.date, "description": newMemer.description, "content": newMemer.content, "socials": newMemer.socials } } export async function toJson(inFile, title, data, description, content, socials) { const rl = readline.createInterface({ input: fs.createReadStream(inFile), crlfDelay: Infinity, }); const lines = []; for await (const line of rl) { lines.push(line); } let object = createNewMember(title, data, description, content, socials); lines.push(JSON.stringify(object)); let string = lines.join('') + ','; writeFile(inFile, string); } function writeFile(inFile, data) { fs.writeFile(inFile, data, { flag: "a+" }, (err) => { if (err) throw err; console.log("The file has been saved!"); } ); }
测试场景说明
posts目录下存放待处理的Markdown文件,路径分别为:
~/path/to/posts/post1.md~/path/to/posts/post2.md~/path/to/posts/post3.md
为简化测试,三个Markdown文件内容完全一致,如下:
--- title: Hello this is a test date: 05/04/2022 description: this is a test socials: --- # Hello ## subhello
目标输出文件路径为~/path/to/post.json,入口执行函数代码如下:
const postDir = path.join(process.cwd(), 'posts'); const destDir = path.join(process.cwd(), '_data'); const jsonFile = 'post.json'; const fileNames = fs.readdirSync(postDir); async function generatePosts(fileNames, postDir, destDir, jsonFile) { fileNames.forEach( (fileName) => { const fullPath = path.join(postDir, fileName); const destPath = path.join(destDir, jsonFile) transformMdtoHtmltoJson(destPath, fullPath); }); } generatePosts(fileNames, postDir, destDir, jsonFile);
问题现象
当前运行得到的post.json输出为多个并列的JSON对象,无外层数组包裹,内容如下:
{ "title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null}, { "title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null}, {"title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null },
期望实现的效果是将所有输出的JSON对象用[]包裹,组成标准JSON数组以便后续遍历,期望输出格式如下:
[ { "title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null}, { "title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null}, {"title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null }, ]
此前尝试的多种方案均错误地给每个单独的JSON对象都包裹了[],得到多个单元素数组并列的错误结果,格式如下:
[{ "title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null}], [{ "title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null}], [{"title":"Hello this is a test", "date":"05/04/2022", "description":"this is a test", "content":"<h1>Hello</h1>\n<h2>subhello</h2>", "socials":null }],
曾尝试使用回调方案解决但未成功,现寻求正确的实现方法。
内容的提问来源于stack exchange,提问作者Gianni Crivello
相关产品推荐
相关产品推荐

