Compare commits

..

No commits in common. '66c50d8f1478363791ddc647b06b36709820a8af' and '04e79c627998e46664ba39f14604560b4bb2a362' have entirely different histories.

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

2
.gitignore vendored

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

7
example_post.yaml

@ -1,7 +0,0 @@
---
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,5 +1,4 @@
const express = require('express');
const matter = require('gray-matter');
const router = express.Router();
const { exec } = require('child_process');
@ -60,36 +59,7 @@ router.get('/viewPost', (req, res) => {
console.log(`stderr: ${stderr}`);
return;
}
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));
res.send(stdout.split('\n').filter(Boolean));
});
});

2
routes/listPosts

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

4
routes/viewPost

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

Loading…
Cancel
Save