unpack-whenwe-json.js -> bin/unpack-whenwe-json.js

Just to be tidy.
This commit is contained in:
Nick Stokoe
2023-05-09 21:23:58 +01:00
parent 5e8be866b4
commit 72a49ac52e

76
bin/unpack-whenwe-json.js Normal file
View File

@@ -0,0 +1,76 @@
/*
Reads in the Drupal node dump of WhenWe data from ./whenwe.json
Writes it converted to markdown with frontmatter, organised in subdirectories, in out/
*/
const data = require('./whenwe.json');
const fs = require('fs');
const path = require('path');
const { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } = require('node-html-markdown');
const nhm = new NodeHtmlMarkdown({
useLinkReferenceDefinitions: true,
useInlineLinks: true,
});
function toYaml(data, body) {
const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ? JSON.stringify(data[key]) : '')).join("\n");
return "---\n" + frontmatter + "\n---\n" + nhm.translate(body);
}
function date(datestr) {
if (datestr) {
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();
}
else
return '';
}
data.forEach((node, ix) => {
const lang = 'und';
const filepath = path.join('out', node.type);
const body = node.body.und[0].value;
const filename = `${node.uuid}.md`;
fs.mkdirSync(path.join(__dirname, filepath), { recursive: true });
const item = {
ix: ix,
nid: Number(node.nid),
type: node.type,
title: node.title,
uuid: node.uuid,
created: new Date(Number(node.created)*1000).toISOString(),
changed: new Date(Number(node.changed)*1000).toISOString(),
path: node.path.alias,
comment_count: node.comment_count,
};
switch(node.type) {
case 'article':
Object.assign(item, {
original_author: node.field_original_author?.und?.[0]?.value,
featured_image: node.field_featured_image?.und?.[0]?.filename,
images: node.field_basic_image_image?.und?.map(item => item.filename),
category: node.field_category?.und?.[0]?.tid,
});
break;
case 'person':
Object.assign(item, {
forename_at_birth: node.field_forename_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_forenames: node.field_other_forenames?.und?.[0]?.value,
title: node.field_title?.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),
parent_of: node.field_parent_of?.und?.[0]?.value,
child_of: node.field_child_of?.und?.[0]?.value,
partner_of: node.field_partner_of?.und?.[0]?.value,
// lifetime: node.field_lifetime?.und?.[0]?.value,
featured_image: node.field_featured_image?.und?.[0]?.filename,
});
break;
}
fs.writeFileSync(path.join(filepath, filename), toYaml(item, body));
console.log(item.title);
});