Skip to content

Commit

Permalink
feat: secret posts
Browse files Browse the repository at this point in the history
  • Loading branch information
linsyking committed Aug 12, 2024
1 parent 015952a commit 486c27d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
.soupault-cache/
node_modules/
.key
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ This new SSG is much faster.
- convert (imagemagick)
- make

Set your personal password for secret posts:

```bash
echo mypassword > .key
```

To build, run:

```bash
Expand Down
6 changes: 6 additions & 0 deletions plugins/post-header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ else
HTML.set_tag_name(post_initdate, "time")
end


is_secret = HTML.select_one(page, "encrypted")
if is_secret then
env["secret"] = is_secret
end

-- Extract and clean up the <post-tags> element
-- It's supposed to look like <post-tags>foo, bar, baz</post-tags>
-- We extract the tags string and split it into individual tags
Expand Down
24 changes: 22 additions & 2 deletions scripts/md.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { md } from "./md-it.mjs";
import { readFileSync, writeFileSync } from "fs";
import { execSync } from "child_process";
import { argv } from "process";
import CryptoJS from "crypto-js";
import jyml from "js-yaml";

function format_dt(dt) {
Expand All @@ -21,6 +22,9 @@ input = input.replaceAll(/\$url\((.*?)\)/g, function (_, p) {
}
});

let is_encrypted = false;
let gen_pp = "";

let opts = {
schema: jyml.JSON_SCHEMA,
};
Expand All @@ -40,6 +44,10 @@ const new_s = input.replace(/^---\n([\s\S]*?)---\n/, function (_, p1) {
let tags = pp1.categories.concat(pp1.tags);
let uniq_tags = [...new Set(tags)];

if (pp1.secret) {
is_encrypted = pp1.secret;
}

if (uniq_tags.length == 0) {
console.error("Warning: empty tag list");
}
Expand Down Expand Up @@ -79,6 +87,7 @@ const new_s = input.replace(/^---\n([\s\S]*?)---\n/, function (_, p1) {
}
}
pp += "\n";
gen_pp = pp;
return pp;
});

Expand All @@ -93,6 +102,17 @@ tags:
new_content += input;
writeFileSync(fpath, new_content);
} else {
var res = md.render(new_s);
console.log(res);
if (is_encrypted) {
const no_pp = input.replace(/^---\n([\s\S]*?)---\n/, "");
let res = md.render(no_pp);
const key = readFileSync(".key").toString();
const encrypted = CryptoJS.AES.encrypt(res, key).toString();
console.log(`${gen_pp}
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="/decrypt.js"></script>
<encrypted data="${encrypted}" />`);
} else {
let res = md.render(new_s);
console.log(res);
}
}
2 changes: 1 addition & 1 deletion site/blog/testsecret.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ tags:

## Test secret content

Hello
Hello world!
27 changes: 27 additions & 0 deletions site/decrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function fun() {
const key = prompt("Enter the key");
if (key) {
this.localStorage.setItem("key", key);
location.reload();
}
}

window.addEventListener("load", function () {
const enc = document.getElementsByTagName("encrypted")[0];
const val = enc.attributes.data.value;
const key = this.localStorage.getItem("key");
if (!key) {
document.getElementsByTagName("main")[0].innerHTML +=
'Error: No key provided. <a onclick="fun()">Click to enter the key.</a>';
return;
}
const decrypted = CryptoJS.AES.decrypt(val, key);
const text = decrypted.toString(CryptoJS.enc.Utf8);
enc.remove();
if (text.length == 0) {
document.getElementsByTagName("main")[0].innerHTML +=
'Error: Incorrect key. <a onclick="fun()">Click to enter the key.</a>';
return;
}
document.getElementsByTagName("main")[0].innerHTML += text;
});
15 changes: 12 additions & 3 deletions soupault.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
excerpt = { selector = ["#post-excerpt", "p"] }
date = { selector = ["#post-date"] }
initdate = { selector = ["#post-initdate"] }
secret = { selector = ["#encrypted"] }
tags = { selector = ".post-tag", select_all = true }

reading_time = { selector = "#reading-time" }
Expand All @@ -111,7 +112,11 @@
# Jingoo template for rendering extracted metadata
index_template = """
{% for e in entries %}
<h2><a href="{{e.url}}">{{e.title}}</a></h2>
<h2><a href="{{e.url}}">{{e.title}}</a>
{% if e.secret %}
<strong>*</strong>
{% endif %}
</h2>
{% if e.date %}
<div><strong>Last update:</strong> {{e.date}}</div>
{% endif %}
Expand Down Expand Up @@ -232,8 +237,12 @@
</div>
</a>
{%- endfor -%}
</div>
{% endif %}
</div>
{% endif %}
{% if secret %}
<div><strong id="encrypted">This is a secret post.</strong></div>
{% endif %}
</div>
'''

Expand Down

0 comments on commit 486c27d

Please sign in to comment.