nm3clol-express-app/views/helpers/functions.js

179 lines
7.9 KiB
JavaScript

const path = require('path');
const glob = require('glob');
const fs = require('fs');
const process = require('process');
const markdownit = require('markdown-it');
var markdownItAttrs = require('markdown-it-attrs');
const md = markdownit({
html: true,
linkify: true,
typographer: true,
}).use(
markdownItAttrs, {
// optional, these are default options
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: [] // empty array = all attributes are allowed
}
);
const moment = require('moment-timezone').tz.setDefault("UTC");
const getSiteName = () => {
return process.env.SITE_NAME||'No Moss 3 Carbo Landfill Online Library';
}
const trimSlashes = ({path}) => {
return path.replace(/^[\/\\]|[\/\\]$/g, '');
};
const getDirectoryName = ({directory}) => {
directory = trimSlashes({path: directory});
let title = trimSlashes({path: directory.replace("public", "")}).replaceAll(path.sep, path.posix.sep);
return (directory=="public") ? getSiteName() : title;
};
const getDirectoryTitle = ({directory}) => {
directory = trimSlashes({path: directory});
let title = trimSlashes({path: directory.replace("public", "")})
.replaceAll(path.sep, path.posix.sep)
.replaceAll('_', ' ')
.split('/')
.reverse()
.join(' - ');
return (directory=="public") ? getSiteName() : `${title} - ${getSiteName()}`;
};
const getWelcomeBanner = ({directory}) => {
//return trimSlashes({path: directory.replace("public", `Welcome to ${getSiteName()}`)});
return "Get Informed! Stay Informed!";
};
const shouldShowDirectorySeparator = ({index}) => (index > 0);
const shouldShowWelcomeBanner = ({paths}) => (paths.length == 1);
const shouldOmitLinkOnLastBreadcrumb = ({paths, index}) => (index == paths.length-1);
const resolveReadmeFile = ({directory}) => {
const resolveFile = (file) => {
const pathToFile = path.join(__dirname, "..", "..", directory, file);
return fs.existsSync(pathToFile) ? pathToFile : "";
};
return (
resolveFile("README.md") ||
resolveFile("README.txt") ||
resolveFile("README") ||
resolveFile("README.html") ||
undefined
);
};
const directoryContainsReadme = ({directory}) => resolveReadmeFile({directory});
const printMarkdownFile = ({file}) => {
};
const printReadme = ({directory}) => {
return md.render(fs.readFileSync(resolveReadmeFile({directory})).toString());
};
const stripWebVTT = (webvttText) => {
const searchHeader = "WEBVTT\nKind: captions\nLanguage: en\n\n";
if (webvttText.startsWith(searchHeader)) {
webvttText = webvttText.substring(searchHeader.length-1); // remove WEBVTT header
webvttText = webvttText.replaceAll(' align:start position:0%', ''); // remove this align and position junk
webvttText = webvttText
.split('\n')
.map((line) => { return line.replaceAll(/.*<\d{2}:\d{2}:\d{2}.\d{3}>.*/g, '').trim() }) // remove all the animated subtitles and trim the whitespace on each line
.join('\n');
while (webvttText.indexOf('\n\n\n') > -1) {
webvttText = webvttText.replace('\n\n\n', '\n\n'); // remove every instance of triple vertical white space, while allowing double vertical white space
}
webvttText = webvttText.replaceAll(/(\d{2}:\d{2}:\d{2}.\d{3}) --> (\d{2}:\d{2}:\d{2}.\d{3})\n(.*)\n\n(\2) --> (\d{2}:\d{2}:\d{2}.\d{3})\n\3\n/g, '$1 --> $5\n$3\n'); // remove every duplicate entry detected
webvttText = webvttText.replaceAll('\n', '<br/>'); // convert \n to <br/>
}
return webvttText;
};
const renderArchive = (html, paths) => {
// Header and Footer content
const headHeaderContent = ``;
const headFooterContent = `
<!-- Dynamically Inserted Code by ${process.env.SITE_NAME||'No Moss 3 Carbo Landfill Online Library'} -->
<style>
.__archived__content__, .__archived__content__ p { background-color: #f44336; color: #fff; font-size: 12pt; font-family: "Noto Serif", Times, "Times New Roman", serif; text-align: center; }
.__archived__content__ h1 { font-family: "Cinzel Decorative", Verdana, Arial, sans-serif; color: #fff; font-weight: 700; font-size: 18pt; text-align: center; }
.__archived__content__ h2 { font-family: "Covered By Your Grace", Verdana, Arial, sans-serif; color: #fff; font-weight: 700; font-size: 18pt; text-align: center; }
.__archived__content__ { padding: 2rem; }
.__archived__content__:first-of-type { margin-top: 0; margin-bottom: 0; }
.__archived__content__:last-of-type { margin-top: 1rem; margin-bottom: 0; }
.__archived__content__ .__archived__content__ p { margin-top: 1rem; }
.__archived__content__ a:link, .__archived__content__ a:visited { color: #fff; }
.__archived__content__ a:hover, .__archived__content__ a:active { text-decoration: none; }
.__archived__content__ ::selection {
background-color: #fff;
color: #f44336;
}
.__archived__content__ ::-moz-selection {
background-color: #fff;
color: #f44336;
}
</style>
<link href="https://fonts.googleapis.com/css?family=Cinzel+Decorative:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Covered+By+Your+Grace:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Noto+Serif:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-HQR3Z1EZQM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-HQR3Z1EZQM');
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8937572456576531" crossorigin="anonymous"></script>
<!-- End dynamically inserted code -->
`;
const bodyHeaderContent = `
<!-- Dynamically Inserted Code by ${process.env.SITE_NAME||'No Moss 3 Carbo Landfill Online Library'} -->
<div class="__archived__content__">
<h1>${process.env.SITE_NAME||'No Moss 3 Carbo Landfill Online Library'}</h1>
<h2>Archived Web Site</h2>
<p>
This is an archived version of the original website. Online features will not be functional. Do not submit any personal information to this archive.
</p>
<p>
<a href="./">Current Directory Listing</a> | <a href="/">Back to Library Home</a>
</p>
</div>
<!-- End dynamically inserted code -->
`;
const bodyFooterContent = `
<!-- Dynamically Inserted Code by ${process.env.SITE_NAME||'No Moss 3 Carbo Landfill Online Library'} -->
<div class="__archived__content__">
<p>
This is an archived version of the original website. Online features will not be functional. Do not submit any personal information to this archive.
</p>
<p>
<a href="./">Current Directory Listing</a> | <a href="/">Back to Library Home</a>
</p>
</div>
<!-- End dynamically inserted code -->
`;
// Add archive branding
html = html.substring(0, html.indexOf('>', html.indexOf('<head')) + '>'.length) + headHeaderContent + html.substring(html.indexOf('>', html.indexOf('<head')) + '>'.length);
html = html.substring(0, html.indexOf('</head>') + '</head>'.length) + headFooterContent + html.substring(html.indexOf('</head>') + '</head>'.length);
html = html.substring(0, html.indexOf('>', html.indexOf('<body')) + '>'.length) + bodyHeaderContent + html.substring(html.indexOf('>', html.indexOf('<body')) + '>'.length);
html = html.substring(0, html.indexOf('</body>') + '</body>'.length) + bodyFooterContent + html.substring(html.indexOf('</body>') + '</body>'.length);
// Output the modified HTML content
return html;
}
module.exports = {
trimSlashes,
getSiteName,
getDirectoryName,
getDirectoryTitle,
getWelcomeBanner,
shouldShowDirectorySeparator,
shouldShowWelcomeBanner,
shouldOmitLinkOnLastBreadcrumb,
directoryContainsReadme,
printReadme,
stripWebVTT,
renderArchive,
md,
moment,
};