unpack-whenwe-json.js - indent, untabify

This commit is contained in:
Nick Stokoe
2023-05-09 12:07:18 +01:00
parent fbfed95657
commit 0f48194d81

View File

@@ -3,70 +3,70 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } = require('node-html-markdown'); const { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } = require('node-html-markdown');
const nhm = new NodeHtmlMarkdown({ const nhm = new NodeHtmlMarkdown({
useLinkReferenceDefinitions: true, useLinkReferenceDefinitions: true,
useInlineLinks: true, useInlineLinks: true,
}); });
function toYaml(data, body) { function toYaml(data, body) {
const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ? '"'+data[key]+'"' : '')).join("\n"); const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ? '"'+data[key]+'"' : '')).join("\n");
return "---\n" + frontmatter + "\n---\n" + nhm.translate(body); return "---\n" + frontmatter + "\n---\n" + nhm.translate(body);
} }
function date(datestr) { function date(datestr) {
if (datestr) { if (datestr) {
const [Y,M,D,h,m,s] = datestr.split(/[^0-9]/) const [Y,M,D,h,m,s] = datestr.split(/[^0-9]/)
return new Date(Date.UTC.call(null, Y,M-1,D,h,m,s)).toISOString(); return new Date(Date.UTC.call(null, Y,M-1,D,h,m,s)).toISOString();
} }
else else
return ''; return '';
} }
data.forEach((node, ix) => { data.forEach((node, ix) => {
const lang = 'und'; const lang = 'und';
const filepath = path.join('out', node.type); const filepath = path.join('out', node.type);
const body = node.body.und[0].value; const body = node.body.und[0].value;
const filename = `${node.uuid}.md`; const filename = `${node.uuid}.md`;
fs.mkdirSync(path.join(__dirname, filepath), { recursive: true }); fs.mkdirSync(path.join(__dirname, filepath), { recursive: true });
const item = { const item = {
ix: ix, ix: ix,
nid: Number(node.nid), nid: Number(node.nid),
type: node.type, type: node.type,
title: node.title, title: node.title,
uuid: node.uuid, uuid: node.uuid,
created: new Date(Number(node.created)*1000).toISOString(), created: new Date(Number(node.created)*1000).toISOString(),
changed: new Date(Number(node.changed)*1000).toISOString(), changed: new Date(Number(node.changed)*1000).toISOString(),
path: node.path.alias, path: node.path.alias,
comment_count: node.comment_count, comment_count: node.comment_count,
}; };
switch(node.type) { switch(node.type) {
case 'article': case 'article':
Object.assign(item, { Object.assign(item, {
original_author: node.field_original_author?.und?.[0]?.value, original_author: node.field_original_author?.und?.[0]?.value,
featured_image: node.field_featured_image?.und?.[0]?.filename, featured_image: node.field_featured_image?.und?.[0]?.filename,
images: node.field_basic_image_image?.und?.map(item => item.filename), images: node.field_basic_image_image?.und?.map(item => item.filename),
category: node.field_category?.und?.[0]?.tid, category: node.field_category?.und?.[0]?.tid,
}); });
break; break;
case 'person': case 'person':
Object.assign(item, { Object.assign(item, {
forename_at_birth: node.field_forename_at_birth?.und?.[0]?.value, forename_at_birth: node.field_forename_at_birth?.und?.[0]?.value,
surname_at_birth: node.field_surname_at_birth?.und?.[0]?.value, surname_at_birth: node.field_surname_at_birth?.und?.[0]?.value,
other_surnames: node.field_other_surnames?.und?.[0]?.value, other_surnames: node.field_other_surnames?.und?.[0]?.value,
other_forenames: node.field_other_forenames?.und?.[0]?.value, other_forenames: node.field_other_forenames?.und?.[0]?.value,
title: node.field_title?.und?.[0]?.value, title: node.field_title?.und?.[0]?.value,
date_of_birth: date(node.field_date_of_birth?.und?.[0]?.value), date_of_birth: date(node.field_date_of_birth?.und?.[0]?.value),
date_of_death: date(node.field_date_of_death?.und?.[0]?.value), date_of_death: date(node.field_date_of_death?.und?.[0]?.value),
parent_of: node.field_parent_of?.und?.[0]?.value, parent_of: node.field_parent_of?.und?.[0]?.value,
child_of: node.field_child_of?.und?.[0]?.value, child_of: node.field_child_of?.und?.[0]?.value,
partner_of: node.field_partner_of?.und?.[0]?.value, partner_of: node.field_partner_of?.und?.[0]?.value,
// lifetime: node.field_lifetime?.und?.[0]?.value, // lifetime: node.field_lifetime?.und?.[0]?.value,
featured_image: node.field_featured_image?.und?.[0]?.filename, featured_image: node.field_featured_image?.und?.[0]?.filename,
}); });
break; break;
} }
fs.writeFileSync(path.join(filepath, filename), toYaml(item, body)); fs.writeFileSync(path.join(filepath, filename), toYaml(item, body));
console.log(item.title); console.log(item.title);
}); });