const path = require('path'); const glob = require('glob'); const fs = require('fs'); const config = require('../../app/config'); 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 = config.getSiteName; 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 getSiteWelcomeMessage = config.getSiteWelcomeMessage; const shouldShowDirectorySeparator = ({index}) => (index > 0); const shouldShowSiteWelcomeMessage = ({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', '
'); // convert \n to
} return webvttText; }; const renderArchive = (html, paths) => { // Header and Footer content const headHeaderContent = ``; const headFooterContent = ` `; const bodyHeaderContent = `

${getSiteName()}

Archived Web Site

This is an archived version of the original website. Online features will not be functional. Do not submit any personal information to this archive.

Current Directory Listing | Back to Library Home

`; const bodyFooterContent = `

This is an archived version of the original website. Online features will not be functional. Do not submit any personal information to this archive.

Current Directory Listing | Back to Library Home

`; // Add archive branding html = html.substring(0, html.indexOf('>', html.indexOf(''.length) + headHeaderContent + html.substring(html.indexOf('>', html.indexOf(''.length); html = html.substring(0, html.indexOf('') + ''.length) + headFooterContent + html.substring(html.indexOf('') + ''.length); html = html.substring(0, html.indexOf('>', html.indexOf(''.length) + bodyHeaderContent + html.substring(html.indexOf('>', html.indexOf(''.length); html = html.substring(0, html.indexOf('') + ''.length) + bodyFooterContent + html.substring(html.indexOf('') + ''.length); // Output the modified HTML content return html; } module.exports = { trimSlashes, getSiteName, getDirectoryName, getDirectoryTitle, getSiteWelcomeMessage, shouldShowDirectorySeparator, shouldShowSiteWelcomeMessage, shouldOmitLinkOnLastBreadcrumb, directoryContainsReadme, printReadme, stripWebVTT, renderArchive, md, moment, };