support hide permalink

This commit is contained in:
leafee98 2023-07-14 15:56:29 +08:00
parent 19f195ba84
commit ba2e6f6376
3 changed files with 105 additions and 23 deletions

View file

@ -15,12 +15,12 @@ highlight = true
[[section.items]] [[section.items]]
name = "Email" name = "Email"
permalink = "mailto:mail@example.org" permalink = "mailto:mail@example.org"
hide = true hide_permalink = true
[[section.items]] [[section.items]]
name = "Matrix" name = "Matrix"
permalink = "https://matrix.to/#/@matrix:example.org" permalink = "https://matrix.to/#/@matrix:example.org"
hide = true hide_permalink = true
[[section]] [[section]]
name = "Sites" name = "Sites"

View file

@ -8,7 +8,7 @@ import express from "express";
const url_path = "/"; const url_path = "/";
const config_path = "config.toml"; const config_path = "config.toml";
const template_dir = "template"; const template_dir = "template/";
const template_path = "index.html.njk"; const template_path = "index.html.njk";
const output_path = "dist/index.html"; const output_path = "dist/index.html";
@ -25,10 +25,21 @@ function main() {
program.help(); program.help();
} }
var config = fs.readFileSync(config_path, {encoding: "utf-8"}); let config = fs.readFileSync(config_path, {encoding: "utf-8"});
config = toml.parse(config) 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, autoescape: true,
throwOnUndefined: true, throwOnUndefined: true,
trimBlocks: true, trimBlocks: true,
@ -36,18 +47,17 @@ function main() {
}); });
if (options.serve) { if (options.serve) {
serve(config); serve(config, nunjucks_env);
} }
if (options.generate) { if (options.generate) {
generate(config); generate(config, nunjucks_env);
} }
}; };
function serve(config, nunjucks_env) {
function serve(config) {
config.dev = true; config.dev = true;
const rendered = nunjucks.render(template_path, config); const rendered = nunjucks_env.render(template_path, config);
const app = express(); const app = express();
const port = 3000; const port = 3000;
@ -67,12 +77,41 @@ function serve(config) {
}); });
} }
function generate(config) { function generate(config, nunjucks_env) {
const rendered = nunjucks.render(template_path, config); const rendered = nunjucks_env.render(template_path, config);
if (! fs.existsSync(path.dirname(output_path))) { if (! fs.existsSync(path.dirname(output_path))) {
fs.mkdirSync(fs.dirname(output_path), { recursive: true }); fs.mkdirSync(fs.dirname(output_path), { recursive: true });
} }
fs.writeFileSync(output_path, rendered); 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(); main();

View file

@ -23,6 +23,41 @@
</script> </script>
{% endmacro %} {% endmacro %}
{% macro unhide_func() %}
<script>
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);
console.log(str);
return str;
}
function unhide_permalink() {
let anchors = document.querySelectorAll("a[hidden_permalink][seed]")
for (let anchor of anchors) {
let arr = new Array();
for (let n of anchor.getAttribute("hidden_permalink").split(",")) {
arr.push(Number(n));
};
let seed = Number(anchor.getAttribute("seed"))
console.log(arr);
console.log(seed);
anchor.href = unhide(seed, arr);
}
}
</script>
{% endmacro %}
{% macro render_item(item) %} {% macro render_item(item) %}
@ -31,12 +66,18 @@
{% set class_str = class_str + " " + "highlight" %} {% set class_str = class_str + " " + "highlight" %}
{% endif %} {% endif %}
{% set href ="/#" %} {% set href = "/#" %}
{% if item.permalink is defined %} {% if item.permalink is defined and not item.hide_permalink %}
{% set href = item.permalink %} {% set href = item.permalink %}
{% endif %} {% endif %}
<a class="{{ class_str }}" href="{{ href | safe }}"> {% 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 %}
<a class="{{ class_str }}" href="{{ href | safe }}" {{ misc_attr | safe }}>
<dl> <dl>
<dt>{{ item.name }}</dt> <dt>{{ item.name }}</dt>
{% if item.description is defined %} {% if item.description is defined %}
@ -69,12 +110,14 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1"> <meta name="viewport" content="width=device-width,minimum-scale=1">
<link ref="canonical" href="{{canonical_link}}" /> <link ref="canonical" href="{{canonical_link}}" />
{{ unhide_func() }}
</head> </head>
<style>{% include "reset.css" %}</style> <style>{% include "reset.css" %}</style>
<style>{% include "style.css" %}</style> <style>{% include "style.css" %}</style>
<body> <body onload="unhide_permalink()">
<main> <main>
<div class="big_title"> <div class="big_title">
<h1>{{ name }}</h1> <h1>{{ name }}</h1>