170 lines
4.8 KiB
TypeScript
170 lines
4.8 KiB
TypeScript
|
import * as blog_posts from './extractions_bdd/helia_wp_posts.json';
|
||
|
|
||
|
import {v4 as uuidv4} from 'uuid';
|
||
|
import moment = require('moment');
|
||
|
|
||
|
const fs = require('fs');
|
||
|
|
||
|
/**
|
||
|
header de fichier org roam
|
||
|
|
||
|
:PROPERTIES:
|
||
|
:ID: ${postuniqID}
|
||
|
:END:
|
||
|
#+title: ${post.post_name}
|
||
|
|
||
|
|
||
|
liens de notes org roam
|
||
|
|
||
|
[[id:720e9d47-f88f-4517-951f-403d47b01c10][blogs]]
|
||
|
|
||
|
** Helia blog [[id:5a4dab45-956b-4a8f-88e2-a50a0cdebe5c][helia]]
|
||
|
|
||
|
**/
|
||
|
|
||
|
// get all posts
|
||
|
// @ts-ignore
|
||
|
console.log('posts length', blog_posts.length);
|
||
|
let ii = 0;
|
||
|
let real_post_ii = 0;
|
||
|
let max_counter = 3000;
|
||
|
// max_counter = 3;
|
||
|
let now: any = new Date();
|
||
|
now = now.getUTCDate();
|
||
|
let blog_name = 'hélia_blog'; // slug
|
||
|
let blog_title = 'hélia blog'; // text title
|
||
|
let liens_bottom = '- hélia [[id:cd68065a-9ede-4c9a-a26c-069c2d51f578][' + blog_title + ']]\n';
|
||
|
let lists_of_links_org = [];
|
||
|
let indexUUID = uuidv4();
|
||
|
|
||
|
var pandoc = require('pandoc');
|
||
|
|
||
|
async function loopOnJson() {
|
||
|
|
||
|
// loop time
|
||
|
|
||
|
|
||
|
// @ts-ignore
|
||
|
blog_posts.map((post: any) => {
|
||
|
ii++;
|
||
|
if (ii < max_counter && (post.post_type == 'post' && (post.post_status == 'publish' || post.post_status == 'draft'))) {
|
||
|
real_post_ii++;
|
||
|
console.log(ii, real_post_ii, 'titre', post.post_name);
|
||
|
|
||
|
let postuniqID = uuidv4();
|
||
|
let sitename = blog_name;
|
||
|
let fileName = (post.post_date.replace(' ', '').replace(/-/g, '').replace(/:/g, '')) + '_' + sitename + '_' + real_post_ii + '_' + post.post_name + '.org';
|
||
|
let contentConverted = '';
|
||
|
let guid = post.post_guid && post.post_guid.length ? '[[' + post.post_guid + ']]' : '';
|
||
|
|
||
|
|
||
|
let excerpt = post.post_excerpt ? post.post_excerpt + '<hr>' : '';
|
||
|
let source = excerpt + post.post_content + ' ';
|
||
|
let picture = post.post_mime_type.includes('image') ? '** Image \n [[img:' + post.post.guid + ']]' : '';
|
||
|
console.log('add link to index', post.post_title);
|
||
|
lists_of_links_org.push({
|
||
|
title: post.post_title,
|
||
|
date: post.post_date,
|
||
|
org_id: postuniqID,
|
||
|
})
|
||
|
console.log('convert source html to Org');
|
||
|
// convert to html with pandoc
|
||
|
pandoc.convert('html', source, ['org'], function (result, err) {
|
||
|
|
||
|
contentConverted = result.org
|
||
|
if (!contentConverted) {
|
||
|
console.log('------- content converted to nothing', source.length, post.post_title);
|
||
|
// console.log('source', source);
|
||
|
contentConverted = '** HTML \n\n' + source + '';
|
||
|
} else {
|
||
|
|
||
|
contentConverted = contentConverted.replace(' \\\\', '');
|
||
|
contentConverted = contentConverted.replace('http:\/\/', 'https://');
|
||
|
}
|
||
|
|
||
|
|
||
|
if (err) {
|
||
|
console.log('pandoc exited with status code ' + err)
|
||
|
} else {
|
||
|
|
||
|
let fileContentConverted = `:PROPERTIES:
|
||
|
:ID: ${postuniqID}
|
||
|
:END:
|
||
|
#+title: "${blog_title} - ${real_post_ii} - ${post.post_title} - ${post.post_status}"
|
||
|
|
||
|
* Article
|
||
|
- ID: ${post.ID}
|
||
|
- guid: ${guid}
|
||
|
- status: ${post.post_status}
|
||
|
- publié le: <${post.post_date ? post.post_date : ''}>
|
||
|
- modifié: <${post.post_modified ? post.post_modified : ''}>
|
||
|
- Index des articles du blog [[id:${indexUUID}][${blog_title}]]
|
||
|
${picture}
|
||
|
** ${post.post_title}
|
||
|
|
||
|
${contentConverted}
|
||
|
|
||
|
* Liens
|
||
|
${liens_bottom}
|
||
|
|
||
|
`;
|
||
|
writeNewFile(fileName, fileContentConverted)
|
||
|
}
|
||
|
|
||
|
|
||
|
});
|
||
|
// @ts-ignore
|
||
|
} else if (ii === blog_posts.length) {
|
||
|
console.log('max_counter atteint', max_counter);
|
||
|
// on last element, create index
|
||
|
makeIndexOfFiles(lists_of_links_org);
|
||
|
}
|
||
|
|
||
|
})
|
||
|
|
||
|
}
|
||
|
|
||
|
function writeNewFile(fileName, fileContent) {
|
||
|
console.log('write file ', fileName);
|
||
|
return fs.writeFile(
|
||
|
`./output/${fileName}`,
|
||
|
fileContent,
|
||
|
"utf8",
|
||
|
(err: any) => {
|
||
|
if (err) {
|
||
|
console.log(`Error writing file: ${err}`);
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
|
||
|
function makeIndexOfFiles(lists_of_links_org) {
|
||
|
console.log('write index', lists_of_links_org.length, 'links');
|
||
|
// make an org file for each
|
||
|
|
||
|
let list_of_links_content = '';
|
||
|
|
||
|
lists_of_links_org.reverse().forEach(pair => {
|
||
|
list_of_links_content += `\n** <${pair.date}> [[id:${pair.org_id}][${pair.title}]]`
|
||
|
})
|
||
|
|
||
|
let file_index_content = `
|
||
|
:PROPERTIES:
|
||
|
:ID: ${indexUUID}
|
||
|
:END:
|
||
|
#+title: ${blog_name}_index
|
||
|
|
||
|
* Index des articles Org
|
||
|
${list_of_links_content}
|
||
|
`;
|
||
|
var today = moment().format('YYYYMMDDHHmmss');
|
||
|
console.log(today, 'list_of_links_content', file_index_content);
|
||
|
writeNewFile(today + '__' + blog_name + '_index.org', file_index_content);
|
||
|
}
|
||
|
|
||
|
loopOnJson().catch(err => console.log(err))
|
||
|
//.then(r => console.log('response', r))
|
||
|
;
|
||
|
|
||
|
|