Это был длинный пост с контекстом, но суть его заключалась в том, что у меня есть один json-файл 2000 элементов с несколькими полями, и мне нужно сделать так, чтобы все они стали их собственными файлами уценки, преобразуя каждое внутреннее поле, кроме элемента определения в передовую Если бы я мог также удалить элемент контента, это было бы бонусом.
Как мне это сделать? Я уже три дня бьюсь головой о стену.
{
"3350" : {
"askedHistPercent1" : "98",
"askedHistYear1" : "2009",
"author" : " Name ",
"categories" : [ "Generic Category" ],
"content" : "<p><br />\n</p>\n<div id=\"sidebar\">\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Keyword_history\">Keyword history</span></h2>\n</div>\n<table class=\"wikitable\">\n<tr>\n<th> Percentage correct\n</th>\n<td> 98%\n</td></tr>\n<tr>\n<th> Year asked\n</th>\n<td> 2009\n</td></tr></table>\n</div>\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Suggest_a_change\">Suggest a change</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span>edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n</div>\n<p><a rel=\"nofollow\" class=\"external text\" href=\"//www.openanesthesia.org/w/index.php?title=Special:PostToApi&kid=1012\">Suggest a change for this keyword</a>\n</p>\n</div>\n<div class=\"visualClear\"></div>\n</div>\n\n<!-- \nNewPP limit report\nCPU time usage: 0.061 seconds\nReal time usage: 0.065 seconds\nPreprocessor visited node count: 68/1000000\nPreprocessor generated node count: 624/1000000\nPost‐expand include size: 840/2097152 bytes\nTemplate argument size: 241/2097152 bytes\nHighest expansion depth: 5/40\nExpensive parser function count: 0/10000\n-->\n\n<!-- Saved in parser cache with key openanes_wiki:pcache:idhash:3245-0!*!*!!en!*!* and timestamp 20141210132538 and revision id 7678\n -->\n",
"date" : "2013-05-30 02:23:04",
"definition" : "<p>Very long definition section;</p>",
"excerpt" : "Keyword history Percentage correct 98% Year asked 2009 Suggest a change[edit] Suggest a change for this keyword",
"id" : 3356,
"permalink" : "",
"sourceLink1" : "Source",
"title" : "Title item name"
},
"3351" : {
"askedHistPercent1" : "98",
"askedHistYear1" : "2009",
"author" : " Name ",
"categories" : [ "Generic Category" ],
"content" : "<p><br />\n</p>\n<div id=\"sidebar\">\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Keyword_history\">Keyword history</span></h2>\n</div>\n<table class=\"wikitable\">\n<tr>\n<th> Percentage correct\n</th>\n<td> 98%\n</td></tr>\n<tr>\n<th> Year asked\n</th>\n<td> 2009\n</td></tr></table>\n</div>\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Suggest_a_change\">Suggest a change</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span>edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n</div>\n<p><a rel=\"nofollow\" class=\"external text\" href=\"//www.openanesthesia.org/w/index.php?title=Special:PostToApi&kid=1012\">Suggest a change for this keyword</a>\n</p>\n</div>\n<div class=\"visualClear\"></div>\n</div>\n\n<!-- \nNewPP limit report\nCPU time usage: 0.061 seconds\nReal time usage: 0.065 seconds\nPreprocessor visited node count: 68/1000000\nPreprocessor generated node count: 624/1000000\nPost‐expand include size: 840/2097152 bytes\nTemplate argument size: 241/2097152 bytes\nHighest expansion depth: 5/40\nExpensive parser function count: 0/10000\n-->\n\n<!-- Saved in parser cache with key openanes_wiki:pcache:idhash:3245-0!*!*!!en!*!* and timestamp 20141210132538 and revision id 7678\n -->\n",
"date" : "2013-05-30 02:23:04",
"definition" : "<p>Very long definition section;</p>",
"excerpt" : "Keyword history Percentage correct 98% Year asked 2009 Suggest a change[edit] Suggest a change for this keyword",
"id" : 3356,
"permalink" : "",
"sourceLink1" : "Source",
"title" : "Title item name"
},
"3356" : {
"askedHistPercent1" : "98",
"askedHistYear1" : "2009",
"author" : " Name ",
"categories" : [ "Generic Category" ],
"content" : "<p><br />\n</p>\n<div id=\"sidebar\">\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Keyword_history\">Keyword history</span></h2>\n</div>\n<table class=\"wikitable\">\n<tr>\n<th> Percentage correct\n</th>\n<td> 98%\n</td></tr>\n<tr>\n<th> Year asked\n</th>\n<td> 2009\n</td></tr></table>\n</div>\n<div class=\"widget\">\n<div class=\"title\">\n<h2><span class=\"mw-headline\" id=\"Suggest_a_change\">Suggest a change</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span>edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n</div>\n<p><a rel=\"nofollow\" class=\"external text\" href=\"//www.openanesthesia.org/w/index.php?title=Special:PostToApi&kid=1012\">Suggest a change for this keyword</a>\n</p>\n</div>\n<div class=\"visualClear\"></div>\n</div>\n\n<!-- \nNewPP limit report\nCPU time usage: 0.061 seconds\nReal time usage: 0.065 seconds\nPreprocessor visited node count: 68/1000000\nPreprocessor generated node count: 624/1000000\nPost‐expand include size: 840/2097152 bytes\nTemplate argument size: 241/2097152 bytes\nHighest expansion depth: 5/40\nExpensive parser function count: 0/10000\n-->\n\n<!-- Saved in parser cache with key openanes_wiki:pcache:idhash:3245-0!*!*!!en!*!* and timestamp 20141210132538 and revision id 7678\n -->\n",
"date" : "2013-05-30 02:23:04",
"definition" : "<p>Very long definition section;</p>",
"excerpt" : "Keyword history Percentage correct 98% Year asked 2009 Suggest a change[edit] Suggest a change for this keyword",
"id" : 3356,
"permalink" : "",
"sourceLink1" : "Source",
"title" : "Title item name"
},
}
Текущее направление. Это отвратительно, но это заставляет меня делать то, что мне нужно. Далее мне нужно выяснить, как добавить только часть описания данных.
const fs = require("fs");
const YAML = require("yamljs");
// const json2md = require("./json2md.js");
const bigObject = require("./data1.json");
// var showdown = require("showdown"),
// converter = new showdown.Converter(),
// text = "# hello, markdown!",
// html = converter.makeHtml(text);
Object.entries(bigObject).forEach(([name, data]) => {
fs.writeFile(`${name}.yaml`, "---\r\n", err => {
fs.appendFile(`${name}.yaml`, YAML.stringify(data), err => {
fs.appendFile(`${name}.yaml`, "---\r\n", err => {
if (err) throw err;
console.log(`${name}.yaml has been saved!`);
});
});
});
});
Вывод одного файла, который я сделал вручную после конвертации с использованием веб-сайта в уценку. Обычно он состоит из всего, что скрыто в frontmatter, а вкладка определения отображается внутри основного корпуса
3350.md
---
title: ATitleNameHere
categories: Category1
property: 3350
id: 3350
date: 2013-05-30
author: Author Name
description: Category1
askedHistPercent1: 75
askedHistPercent2: 76
askedHistYear1: 2012
askedHistYear2: 2009
assocMediaLink1: Optional section here
assocMediaCaption1: Second Optional section here
---
Definition item here. Usually quite long.