Skip to content

Instantly share code, notes, and snippets.

@Jytesh
Last active January 7, 2021 16:54
Show Gist options
  • Save Jytesh/ce7e87b11fe041f4fb3b9a0e5687b319 to your computer and use it in GitHub Desktop.
Save Jytesh/ce7e87b11fe041f4fb3b9a0e5687b319 to your computer and use it in GitHub Desktop.
Makes wiki txt in root to md
const
server = require('express')()
server.get('/*',(req,res)=>{
if(req.url){
res.sendFile(__dirname+'/krew-wiki/docs/'+req.url)
}
else res.sendFile(__dirname+'/krew-wiki/docs/index.html')
})
server.listen(8080)
console.log('start listening in port: http://localhost:8080')
const fs = require('fs');
fs.readdir('./','ascii',(err,dir)=>{
if(!dir) return console.log('Nothing found tf')
if(err) console.log(err)
txts = dir.filter(t=>t.split('.').pop() == 'txt');
txts.forEach(txt=>{
fs.readFile(txt,(err,text)=>{
if(err) console.log(err)
text = text.toString()
//Headers
text = text.replace(/#\s/g,'* ')
//Bold
text = text.replace(/'''(.*)'''/gm,'**$1**')
text = text.replace(/=====\s*(.*)\s*=====/gm, "#### $1\n\n");
text = text.replace(/====\s*(.*)\s*====/gm, "### $1\n\n");
text = text.replace(/===\s*(.*)\s*===/gm, "## $1");
text = text.replace(/==\s*(.*)\s*==/gm, "# $1");
// '[^\s]*' matches the URL, '([^\]]*)' machtes everything else till the first closing brakcet ']'
//text = text.replace(/\[([^\s]*)\s([^\]]*)\]/g, "[$2]($1)");
//NEW url convertor
/*
template:
[[Crabs]] -> /[Crabs](/crabs.md)
[[Crab|Crabs]] --> [Crab](./crabs.md)
*/
urls = (text).match(/\[\[([^\]]*)\]\]/gm)
if(urls)urls.forEach(t=>{
if(~t.indexOf('File')) return console.log('NEED UPLOAD FOR ',t)
newT = t.replace(/\[\[(.*)\]\]/,'$1')
pos = newT.indexOf('|')
let link,name
[name,link] = ~pos ? [newT.substring(0,pos),newT.substring(pos+1)] : [newT,newT]
text = text.replace(t,`[${name}](./${encodeURIComponent(link)}.md)`)
}
)
if(~process.argv.join(' ').indexOf('--removeTemplates')) text= text.replace(/{{(.*)}}/g,'')
// convert '**' into ' *'
text = text.replace(/\*\*/gm, " *");
fs.writeFile(txt.replace('txt','md'),text,()=>{})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment