support hide permalink
This commit is contained in:
parent
19f195ba84
commit
ba2e6f6376
|
@ -15,12 +15,12 @@ highlight = true
|
|||
[[section.items]]
|
||||
name = "Email"
|
||||
permalink = "mailto:mail@example.org"
|
||||
hide = true
|
||||
hide_permalink = true
|
||||
|
||||
[[section.items]]
|
||||
name = "Matrix"
|
||||
permalink = "https://matrix.to/#/@matrix:example.org"
|
||||
hide = true
|
||||
hide_permalink = true
|
||||
|
||||
[[section]]
|
||||
name = "Sites"
|
||||
|
|
59
index.js
59
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();
|
||||
|
|
|
@ -23,6 +23,41 @@
|
|||
</script>
|
||||
{% 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) %}
|
||||
|
||||
|
@ -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 %}
|
||||
|
||||
<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>
|
||||
<dt>{{ item.name }}</dt>
|
||||
{% if item.description is defined %}
|
||||
|
@ -69,12 +110,14 @@
|
|||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,minimum-scale=1">
|
||||
<link ref="canonical" href="{{canonical_link}}" />
|
||||
|
||||
{{ unhide_func() }}
|
||||
</head>
|
||||
|
||||
<style>{% include "reset.css" %}</style>
|
||||
<style>{% include "style.css" %}</style>
|
||||
|
||||
<body>
|
||||
<body onload="unhide_permalink()">
|
||||
<main>
|
||||
<div class="big_title">
|
||||
<h1>{{ name }}</h1>
|
||||
|
|
Loading…
Reference in a new issue