Compare commits

...

5 Commits

Author SHA1 Message Date
Nick Stokoe
d78b403dff unpack-whenwe-json.js - convert html to markdown 2022-12-27 18:16:27 +00:00
Nick Stokoe
063c6742b2 .gitignore /node_modules/ 2022-12-27 18:16:06 +00:00
Nick Stokoe
a154e21f8b .gitignore /out/ 2022-12-27 18:11:13 +00:00
Nick Stokoe
013ca966ef unpack-whenwe-json.js - unpacks a drupal node-export of whenwe's nodes 2022-12-27 18:11:08 +00:00
Nick Stokoe
b6a9eddffa package.json - initial version 2022-12-27 18:08:00 +00:00
3 changed files with 85 additions and 0 deletions

2
.gitignore vendored
View File

@@ -0,0 +1,2 @@
/out/
/node_modules/

14
package.json Normal file
View File

@@ -0,0 +1,14 @@
{
"name": "conversion",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"node-html-markdown": "^1.3.0"
}
}

69
unpack-whenwe-json.js Normal file
View File

@@ -0,0 +1,69 @@
const data = require('./whenwe.json');
const fs = require('fs');
const path = require('path');
const { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } = require('node-html-markdown');
const nhm = new NodeHtmlMarkdown();
function toYaml(data, body) {
const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ?? '')).join("\n");
return 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}.yml`;
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);
});