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

98 lines
3.6 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 '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);
return (directory=="public") ? getSiteName() : `${title} Listing - ${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(process.cwd(), "..", directory, file)
return fs.existsSync(pathToFile) ? pathToFile : "";
};
return (
resolveFile("README.md") ||
resolveFile("README.txt") ||
resolveFile("README") ||
resolveFile("README.html") ||
""
);
};
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;
};
module.exports = {
trimSlashes,
getSiteName,
getDirectoryName,
getDirectoryTitle,
getWelcomeBanner,
shouldShowDirectorySeparator,
shouldShowWelcomeBanner,
shouldOmitLinkOnLastBreadcrumb,
directoryContainsReadme,
printReadme,
stripWebVTT,
md,
moment,
};