78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
const path = require('path');
|
|
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()}`)});
|
|
};
|
|
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());
|
|
};
|
|
|
|
module.exports = {
|
|
trimSlashes,
|
|
getSiteName,
|
|
getDirectoryName,
|
|
getDirectoryTitle,
|
|
getWelcomeBanner,
|
|
shouldShowDirectorySeparator,
|
|
shouldShowWelcomeBanner,
|
|
shouldOmitLinkOnLastBreadcrumb,
|
|
directoryContainsReadme,
|
|
printReadme,
|
|
md,
|
|
moment,
|
|
}; |