@ -3,7 +3,7 @@ const parser = require("fast-xml-parser");
const { writeFileSync , existsSync , mkdirSync } = require ( 'fs' ) ;
const ent = require ( 'ent' )
const { getFiles } = require ( './file-utils' ) ;
const moment = require ( 'moment' )
function stripBeforeThirdSlash ( str ) {
const splitted = str . split ( '/' )
@ -14,20 +14,16 @@ function stripBeforeLastSlash(str) {
return str . substring ( str . lastIndexOf ( '/' ) + 1 , str . length )
}
function strpad ( n ) {
return String ( "0" + n ) . slice ( - 2 ) ;
}
function convertAtomItemToMd ( item , opts ) {
const path = ` ${ opts . notesdir } / ${ item . year } / ${ item . month } `
if ( ! existsSync ( ` ${ opts . notesdir } / ${ item . year } ` ) ) mkdirSync ( ` ${ opts . notesdir } / ${ item . year } ` )
function convertAtomItemToMd ( item , notesdir ) {
const path = ` ${ notesdir } / ${ item . year } / ${ item . month } `
if ( ! existsSync ( ` ${ notesdir } / ${ item . year } ` ) ) mkdirSync ( ` ${ notesdir } / ${ item . year } ` )
if ( ! existsSync ( path ) ) mkdirSync ( path )
const mddata = ` ---
source : "${item.url}"
context : "${item.context}"
title : "${item.title}"
date : "${item.year}-${item.month}-${item.day}T${ strpad(item.date.getHours())}:${strpad(item.date.getMinutes())}:${strpad(item.date.getSeconds() )}"
date : "${item.year}-${item.month}-${item.day}T${ item.date.format("HH : mm : ss " )}"
-- -
$ { item . content }
@ -39,14 +35,17 @@ ${item.content}
// opts:
// notesdir = `${__dirname}/content/notes`
// url = "https://chat.brainbaking.com/users/wouter/feed";
// utcOffset = "+01:00"
async function parseMastoFeed ( options ) {
const { notesdir , url , utcOffset = "+01:00" } = options
async function parseMastoFeed ( opts ) {
const notesroot = await getFiles ( opts . notesdir )
const notesroot = await getFiles ( notesdir )
const notes = notesroot
. filter ( name => name . endsWith ( '.md' ) )
. map ( n => stripBeforeThirdSlash ( n . replace ( '.md' , '' ) ) )
const buffer = await got ( opts. url, {
const buffer = await got ( url, {
responseType : "buffer" ,
resolveBodyOnly : true ,
timeout : 5000 ,
@ -56,10 +55,10 @@ async function parseMastoFeed(opts) {
ignoreAttributes : false
} )
const items = root . feed . entry . map ( item => {
const date = new Date ( item . published )
const year = date . getFullYear( )
const month = strpad( date . getMonth ( ) + 1 )
const day = strpad( date . getDate ( ) )
const date = moment . utc ( item . published ) . utcOffset ( utcOffset )
const year = date . format( "YYYY" )
const month = date. format ( "MM" )
const day = date. format ( "DD" )
// format: <thr:in-reply-to ref='https://social.linux.pizza/users/StampedingLonghorn/statuses/105821099684887793' href='https://social.linux.pizza/users/StampedingLonghorn/statuses/105821099684887793'/>
const context = item [ 'thr:in-reply-to' ] ? item [ 'thr:in-reply-to' ] [ '@_ref' ] : ""
@ -69,7 +68,7 @@ async function parseMastoFeed(opts) {
url : item . id , // format: https://chat.brainbaking.com/objects/0707fd54-185d-4ee7-9204-be370d57663c
context ,
id : stripBeforeLastSlash ( item . id ) ,
hash : ` ${ day } h ${ date . getHours( ) } m ${ date . getMinutes ( ) } s ${ date . getSeconds ( ) } ` ,
hash : ` ${ day } h ${ date . format( "HH" ) } m ${ date . format ( "mm" ) } s ${ date . format ( "ss" ) } ` ,
date , // format: 2021-03-02T16:18:46.658056Z
year ,
month ,
@ -77,7 +76,7 @@ async function parseMastoFeed(opts) {
}
} )
. filter ( itm => ! notes . includes ( ` ${ itm . year } / ${ itm . month } / ${ itm . hash } ` ) )
. forEach ( itm => convertAtomItemToMd ( itm , op ts) )
. forEach ( itm => convertAtomItemToMd ( itm , n ote sdir ) )
}
module . exports = {