diff --git a/unpack-whenwe-json.js b/unpack-whenwe-json.js index dddf0e3..7992ea1 100644 --- a/unpack-whenwe-json.js +++ b/unpack-whenwe-json.js @@ -3,70 +3,70 @@ const fs = require('fs'); const path = require('path'); const { NodeHtmlMarkdown, NodeHtmlMarkdownOptions } = require('node-html-markdown'); const nhm = new NodeHtmlMarkdown({ - useLinkReferenceDefinitions: true, - useInlineLinks: true, + useLinkReferenceDefinitions: true, + useInlineLinks: true, }); function toYaml(data, body) { - const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ? '"'+data[key]+'"' : '')).join("\n"); - return "---\n" + frontmatter + "\n---\n" + nhm.translate(body); + const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ? '"'+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 ''; + 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); + 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); });