summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMhykol <mchaeldonald62@pm.me>2024-05-08 04:52:03 -0400
committerMhykol <mchaeldonald62@pm.me>2024-05-08 04:52:03 -0400
commit5b4e5781440ae32756fc7dd30e6bf4f0847c3a01 (patch)
tree987b9da55be99c68ebcbf10abc24421b0709c549 /source
parent91b47111035e3af270bda77859fff82ccb774a12 (diff)
Preserve leading whitespace in markdown files and improve error handling and variable declarations
Diffstat (limited to 'source')
-rw-r--r--source/blog.js37
-rw-r--r--source/readfile.js2
2 files changed, 23 insertions, 16 deletions
diff --git a/source/blog.js b/source/blog.js
index 57735b7..8e0922f 100644
--- a/source/blog.js
+++ b/source/blog.js
@@ -1,7 +1,11 @@
const fs = require('fs')
const path = require('path')
const markdownit = require('markdown-it')
-const md = markdownit()
+const md = markdownit({
+ typographer: {
+ whitespace: 'all'
+ }
+})
const logger = require('./logger')()
// Parse markdown
@@ -20,27 +24,30 @@ class Blog {
this.blog = new Map()
files.forEach(file => {
- this.blog.set(file.replace('.md', ''), fs.readFileSync(path.resolve(`./blog/${file}`), 'utf-8', (err, data) => {
- if (err) {
- logger.Error(err)
- return
- }
- return data
- }))
+ try {
+ this.blog.set(file.replace('.md', ''), fs.readFileSync(path.resolve(`./blog/${file}`), 'utf-8', (err, data) => {
+ if (err) {
+ logger.Error(err)
+ return
+ }
+ return data
+ }))
+ } catch (err) {
+ logger.Error(err)
+ }
})
this.blog.forEach((value, key) => {
- this.regex = /# (.*)\nDate: (.*)\n---(.*?)---/s
- this.match = value.match(this.regex)
+ const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s
+ const match = value.match(regex)
- if (this.match) {
- const [, title, date, description] = this.match
- this.object = {
+ if (match) {
+ const [, title, date, description] = match
+ this.blog.set(key, {
title: title,
date: new Date(date),
short: description,
body: md.render(value)
- }
- this.blog.set(key, this.object)
+ })
} else {
this.blog.set(key, {
title: undefined,
diff --git a/source/readfile.js b/source/readfile.js
index c3970f6..1f332d0 100644
--- a/source/readfile.js
+++ b/source/readfile.js
@@ -102,7 +102,7 @@ class ReadFile {
}
async #GetFileType(x) {return this.object.types[path.extname('./assets/'+ x[1] + '/' + x[2]).slice(1)] || 'text/plain'}
#Build() {return fs.readFileSync('./views/layouts/header.html', 'utf-8') + fs.readFileSync('./views/layouts/footer.html', 'utf-8')}
- #RemoveSpaces(x) {return x.replace(/(\n)\s+/g, '$1')}
+ #RemoveSpaces(x) {return x.replace(/^(\n)\s+/gm, '$1')}
}
module.exports = ReadFile