self contained code highlight

This commit is contained in:
Evert Prants 2022-01-03 21:53:56 +02:00
parent 14aec15736
commit df314a2226
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 109 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,15 +1,24 @@
const express = require('express')
const path = require('path')
const fs = require('fs')
const crypto = require('crypto')
const fsp = fs.promises
const router = express.Router()
const cfgLoader = require(path.join('..', '..', 'config-loader'))(path.join(__dirname, 'config.json'), {
root: path.join(process.cwd(), '..', 'fiche', 'code'),
style: 'tomorrow-night',
index: 'index.html'
index: 'index.html',
})
function calculateHash(file, type){
const testFile = fs.readFileSync(file);
const shasum = crypto.createHash('sha384')
.update(testFile)
.digest('base64');
return `sha384-${shasum}`
}
function encodeSpecial (string) {
let i = string.length
const a = []
@ -28,9 +37,24 @@ function encodeSpecial (string) {
async function init () {
const config = await cfgLoader
const root = path.resolve(config.root)
const themeFile = path.join(__dirname, `${config.style}.css`)
const scriptFile = path.join(__dirname, 'highlight.min.js')
const themeFileHash = calculateHash(themeFile)
const scriptFileHash = calculateHash(scriptFile)
await fsp.access(root, fs.constants.F_OK)
router.get('/theme.css', (req, res) => {
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
res.sendFile(themeFile)
});
router.get('/script.js', (req, res) => {
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
res.sendFile(scriptFile)
});
router.get('/:name', async (req, res, next) => {
const name = req.params.name
if (name.length > 5) {
@ -67,9 +91,8 @@ async function init () {
<!DOCTYPE html>
<html class="hljs">
<head>
<link rel="stylesheet" href="/assets/css/t.css" />
<link rel="stylesheet" href="/assets/css/t-styles/${config.style}.css" />
<script src="/assets/js/highlight.min.js"></script>
<link rel="stylesheet" href="theme.css" integrity="${themeFileHash}" />
<script src="script.js" integrity="${scriptFileHash}"></script>
</head>
<body>
<pre><code>${etext}</code></pre>

View File

@ -0,0 +1,79 @@
/* Tomorrow Night Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
code {
white-space: pre;
}
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #969896;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #cc6666;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #de935f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #f0c674;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #b5bd68;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #81a2be;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #b294bb;
}
.hljs {
display: block;
overflow-x: auto;
background: #1d1f21;
color: #c5c8c6;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}