From ba2e6f63766fd0d7df2703914ffd80a3a64fe8f0 Mon Sep 17 00:00:00 2001 From: leafee98 Date: Fri, 14 Jul 2023 15:56:29 +0800 Subject: [PATCH] support hide permalink --- config.toml | 18 ++++++------- index.js | 59 ++++++++++++++++++++++++++++++++++------- template/index.html.njk | 51 ++++++++++++++++++++++++++++++++--- 3 files changed, 105 insertions(+), 23 deletions(-) diff --git a/config.toml b/config.toml index e961811..db37b66 100644 --- a/config.toml +++ b/config.toml @@ -8,19 +8,19 @@ canonical_link = "https://leafee98.com/" name = "Social" [[section.items]] -name = "GitHub" -permalink = "https://github.com/leafee98" -highlight = true +name = "GitHub" +permalink = "https://github.com/leafee98" +highlight = true [[section.items]] -name = "Email" -permalink = "mailto:mail@example.org" -hide = true +name = "Email" +permalink = "mailto:mail@example.org" +hide_permalink = true [[section.items]] -name = "Matrix" -permalink = "https://matrix.to/#/@matrix:example.org" -hide = true +name = "Matrix" +permalink = "https://matrix.to/#/@matrix:example.org" +hide_permalink = true [[section]] name = "Sites" diff --git a/index.js b/index.js index 48a8b04..f6e85f5 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ import express from "express"; const url_path = "/"; const config_path = "config.toml"; -const template_dir = "template"; +const template_dir = "template/"; const template_path = "index.html.njk"; const output_path = "dist/index.html"; @@ -25,10 +25,21 @@ function main() { program.help(); } - var config = fs.readFileSync(config_path, {encoding: "utf-8"}); + let config = fs.readFileSync(config_path, {encoding: "utf-8"}); config = toml.parse(config) - nunjucks.configure(template_dir, { + for (let i = 0; i < config["section"].length; i++) { + for (let j = 0; j < config["section"][i]["items"].length; j++) { + let item = config["section"][i]["items"][j]; + if (item.hide_permalink) { + let seed = Math.floor(Math.random() * 1000000007) + item.permalink = hide(seed, item.permalink); + item.seed = seed; + } + } + } + + let nunjucks_env = nunjucks.configure(template_dir, { autoescape: true, throwOnUndefined: true, trimBlocks: true, @@ -36,18 +47,17 @@ function main() { }); if (options.serve) { - serve(config); + serve(config, nunjucks_env); } if (options.generate) { - generate(config); + generate(config, nunjucks_env); } }; - -function serve(config) { +function serve(config, nunjucks_env) { config.dev = true; - const rendered = nunjucks.render(template_path, config); + const rendered = nunjucks_env.render(template_path, config); const app = express(); const port = 3000; @@ -67,12 +77,41 @@ function serve(config) { }); } -function generate(config) { - const rendered = nunjucks.render(template_path, config); +function generate(config, nunjucks_env) { + const rendered = nunjucks_env.render(template_path, config); if (! fs.existsSync(path.dirname(output_path))) { fs.mkdirSync(fs.dirname(output_path), { recursive: true }); } fs.writeFileSync(output_path, rendered); } +// A simple function to encrypt string +// to avoid being collected by robots. +function hide(seed, str) { + const str_arr = new TextEncoder().encode(str); + const res_arr = []; + seed %= 1000000007; + + for (let i = 0; i < str_arr.length; i++) { + res_arr.push((str_arr[i] + seed) % 256); + seed = seed * seed % 1000000007; + } + + + return res_arr; +} + +function unhide(seed, arr) { + seed %= 1000000007; + const str_arr = []; + for (let i = 0; i < arr.length; i++) { + str_arr.push((((arr[i] - seed) % 256) + 256) % 256) + seed = seed * seed % 1000000007; + } + + const ui8_arr = new Uint8Array(str_arr); + const str = new TextDecoder().decode(ui8_arr); + return str; +} + main(); diff --git a/template/index.html.njk b/template/index.html.njk index cc9c412..43da18a 100644 --- a/template/index.html.njk +++ b/template/index.html.njk @@ -23,6 +23,41 @@ {% endmacro %} +{% macro unhide_func() %} + +{% endmacro %} + {% macro render_item(item) %} @@ -31,12 +66,18 @@ {% set class_str = class_str + " " + "highlight" %} {% endif %} -{% set href ="/#" %} -{% if item.permalink is defined %} +{% set href = "/#" %} +{% if item.permalink is defined and not item.hide_permalink %} {% set href = item.permalink %} {% endif %} - +{% set misc_attr = "" %} +{% if item.hide_permalink %} +{% set misc_attr = misc_attr + " " + "hidden_permalink=\"" + item.permalink + "\"" %} +{% set misc_attr = misc_attr + " " + "seed=\"" + item.seed + "\"" %} +{% endif %} + +
{{ item.name }}
{% if item.description is defined %} @@ -69,12 +110,14 @@ + + {{ unhide_func() }} - +

{{ name }}