Compare commits

..

5 Commits

  1. 2
      .gitignore
  2. 7
      example_post.yaml
  3. 32
      index.js
  4. 2
      routes/listPosts
  5. 4
      routes/viewPost

2
.gitignore vendored

@ -0,0 +1,2 @@
package-lock.json
node_modules/

7
example_post.yaml

@ -0,0 +1,7 @@
---
title: "This is a post!"
author: "Me"
timestamp: "1692975412936"
type: "text/plain"
---
It can have an arbitrary number of attributes, followed by a content body.

32
index.js

@ -1,4 +1,5 @@
const express = require('express');
const matter = require('gray-matter');
const router = express.Router();
const { exec } = require('child_process');
@ -59,7 +60,36 @@ router.get('/viewPost', (req, res) => {
console.log(`stderr: ${stderr}`);
return;
}
res.send(stdout.split('\n').filter(Boolean));
let post = matter(stdout)
let { title, author, timestamp } = post.data
let html=""
if ( title ) {
html += `<h1>${title}</h1>`
}
if ( author && timestamp ) {
let date = new Date(parseInt(timestamp)).toLocaleString()
html += `<header>Created by ${author} on ${date}</header>`
}
html += '<hr/>'
html += `<p>${post.content}</p>`
res.send(html);
});
});
router.get('/api/viewPost', (req, res) => {
let { filename } = req.query;
exec(`./routes/viewPost --filename=${filename}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
res.json(matter(stdout));
});
});

2
routes/listPosts

@ -7,4 +7,4 @@ limit=$3
offset=$(( (page - 1) * limit ))
ls -1 "$dir"/*.yaml | awk -F/ '{print $NF}' | sed 's/\.yaml$//' | tail -n "+$((offset+1))" | head -n "$limit"
ls -1 "$dir"/*.yaml | awk -F/ '{print $NF}' | tail -n "+$((offset+1))" | head -n "$limit"

4
routes/viewPost

@ -1,12 +1,12 @@
#!/bin/sh
# usage: ./viewPost.sh --file=<filename>
# usage: ./viewPost.sh --filename=<filename>
# Parse the arguments
for arg in "$@"
do
case $arg in
--file=*)
--filename=*)
file="${arg#*=}"
;;
esac

Loading…
Cancel
Save