fix: Fixed side effects from hardcoded paths.

This commit is contained in:
David Ball 2024-06-15 16:38:17 -04:00
parent 5f4ac46af7
commit 9cb16680f9
8 changed files with 71 additions and 43 deletions

View File

@ -3,12 +3,13 @@ console.log(`Configuring .env and expanding .env to include environment variable
const path = require('path');
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
const process = require('process');
let env = dotenv.config();
dotenvExpand.expand(env);
const getAppHttpHost = () => env.APP_HTTP_HOST||'nm3clol-express-app';
const getAppHttpPort = () => parseInt(env.APP_HTTP_PORT||env.PORT||3000);
const getAppHttpPort = () => process.env.PORT||env.APP_HTTP_PORT||3000;
const getAppHttpUrl = () => {
return env.APP_HTTP_URL || `http://${getAppHttpHost() + ((getAppHttpPort() === 80) ? '' : ':' + getAppHttpPort())}`
};
@ -30,20 +31,24 @@ const getSolrLawPort = () => parseInt(env.SOLR_LAW_PORT||getSolrDocsPort());
const getSolrLawCore = () => env.SOLR_LAW_CORE||'vacode_core';
module.exports = {
config: {
publicPath: getPublicPath(),
pagesPath: getPagesPath(),
staticPath: getStaticPath(),
config: {
appHttpHost: getAppHttpHost(),
appHttpPort: getAppHttpPort(),
appHttpUrl: getAppHttpUrl(),
siteName: getSiteName(),
siteWelcomeMessage: getSiteWelcomeMessage(),
siteHost: getSiteHost(),
siteUrl: getSiteUrl(),
appHttpHost: getAppHttpHost(),
appHttpPort: getAppHttpPort(),
appHttpUrl: getAppHttpUrl(),
publicPath: getPublicPath(),
pagesPath: getPagesPath(),
staticPath: getStaticPath(),
solrDocsHost: getSolrDocsHost(),
solrDocsPort: getSolrDocsPort(),
solrDocsCore: getSolrDocsCore(),
solrLawHost: getSolrLawHost(),
solrLawPort: getSolrLawPort(),
solrLawCore: getSolrLawCore(),

View File

@ -88,7 +88,7 @@ glob.globSync('**/*.md', {
const fmData = { fm: fm.data, excerpt: fm.excerpt };
const content = helpers.md.render(fm.content, fmData );
const renderData = { content, route, filePath, fullFilePath, req, paths, ...fmData };
res.render("page", { h: helpers, ...renderData });
res.render("page", { h: helpers, require, ...renderData });
});
});

View File

@ -25,7 +25,7 @@ const helpers = require('../views/helpers/functions');
// const errorTemplate = require('./views/error');
const directoryTemplate = (vals) => {
return new Promise((resolve, reject) => {
ejs.renderFile(path.join(__dirname, '..', 'views', 'directory.ejs'), { h: helpers, ...vals }, (err, str) => {
ejs.renderFile(path.join(__dirname, '..', 'views', 'directory.ejs'), { h: helpers, require, ...vals }, (err, str) => {
if (err) {
reject(err);
} else {
@ -36,7 +36,7 @@ const directoryTemplate = (vals) => {
};
const errorTemplate = (vals) => {
return new Promise((resolve, reject) => {
ejs.renderFile(path.join(__dirname, '..', 'views', 'error.ejs'), { h: helpers, ...vals }, (err, str) => {
ejs.renderFile(path.join(__dirname, '..', 'views', 'error.ejs'), { h: helpers, require, ...vals }, (err, str) => {
if (err) {
reject(err);
} else {

View File

@ -22,7 +22,9 @@
<i>&nbsp;</i>
<%= h.getSiteWelcomeMessage() %>
<% } else if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
<%= h.trimSlashes({path: value.name}).replaceAll('_', ' ') %>
<%= h.trimSlashes(value.name).replaceAll('_', ' ') %>
<% } else if (index == 0) { %>
<a href="/"><%= h.getSiteName() %></a>
<% } else { %>
<a href="/<%= value.url %>">
<%= h.getDirectoryName({directory: value.name}).replaceAll('_', ' ') %>

View File

@ -1,7 +1,7 @@
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const config = require('../../app/config');
const { config } = require('../../app/config');
const markdownit = require('markdown-it');
var markdownItAttrs = require('markdown-it-attrs');
const md = markdownit({
@ -18,34 +18,50 @@ const md = markdownit({
);
const moment = require('moment-timezone').tz.setDefault("UTC");
const getSiteName = config.getSiteName;
const getSiteName = () => config.siteName;
const trimSlashes = ({path}) => {
return path.replace(/^[\/\\]|[\/\\]$/g, '');
const trimSlashes = (dirPath) => {
return dirPath.replace(/^[\/\\]|[\/\\]$/g, '');
};
const getLeftMostDirectory = (directory) => {
if (directory.indexOf(path.sep)) {
return directory.substring(0, directory.indexOf(path.sep));
}
return directory;
}
const leftTrimFirstDirectory = (directory) => {
if (directory.indexOf(path.sep)) {
return directory.substring(directory.indexOf(path.sep));
}
return directory;
}
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;
directory = trimSlashes(directory);
const leftMostDirectory = getLeftMostDirectory(directory);
let title = trimSlashes(leftTrimFirstDirectory(directory)).replaceAll(path.sep, path.posix.sep);
return (trimSlashes(directory)==leftMostDirectory) ? getSiteName() : title;
};
const getDirectoryTitle = ({directory}) => {
directory = trimSlashes({path: directory});
let title = trimSlashes({path: directory.replace("public", "")})
const leftMostDirectory = getLeftMostDirectory(directory);
let title = trimSlashes(leftTrimFirstDirectory(directory))
.replaceAll(path.sep, path.posix.sep)
.replaceAll('_', ' ')
.split('/')
.reverse()
.join(' - ');
return (directory=="public") ? getSiteName() : `${title} - ${getSiteName()}`;
return (trimSlashes(directory)==leftMostDirectory) ? getSiteName() : `${title} - ${getSiteName()}`;
};
const getSiteWelcomeMessage = config.getSiteWelcomeMessage;
const getSiteWelcomeMessage = () => config.siteWelcomeMessage;
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);
const pathToFile = path.join(config.publicPath, trimSlashes(leftTrimFirstDirectory(directory)), file);
return fs.existsSync(pathToFile) ? pathToFile : "";
};
return (
@ -157,18 +173,19 @@ const renderArchive = (html, paths) => {
}
module.exports = {
trimSlashes,
getSiteName,
getDirectoryName,
getDirectoryTitle,
getSiteWelcomeMessage,
shouldShowDirectorySeparator,
shouldShowSiteWelcomeMessage,
shouldOmitLinkOnLastBreadcrumb,
directoryContainsReadme,
printReadme,
stripWebVTT,
renderArchive,
md,
moment,
leftTrimFirstDirectory,
trimSlashes,
getSiteName,
getDirectoryName,
getDirectoryTitle,
getSiteWelcomeMessage,
shouldShowDirectorySeparator,
shouldShowSiteWelcomeMessage,
shouldOmitLinkOnLastBreadcrumb,
directoryContainsReadme,
printReadme,
stripWebVTT,
renderArchive,
md,
moment,
};

View File

@ -20,8 +20,10 @@
<% } %>
<% if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
<%= (typeof fm.title !== 'undefined') ? `${fm.title}` : value %>
<% } else { %>
<a href="/<%= value.replace('public', '') != '' ? value.replace('public', '') : '' %>">
<% } else if (index == 0) { %>
<a href="/"><%= h.getSiteName() %></a>
<% } else { %>
<a href="/<%= h.trimSlashes(h.leftTrimFirstDirectory(value)) %>">
<%= (value == 'public' ? h.getSiteName() : value) %>
</a>
<% } %>

View File

@ -21,8 +21,10 @@
Welcome to <%= h.getDirectoryTitle({directory}) %>
<% } else if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
<%= h.trimSlashes({path: value.name}) %>
<% } else { %>
<a href="/<%= value.url %>">
<% } else if (index == 0) { %>
<a href="/"><%= h.getSiteName() %></a>
<% } else { %>
<a href="/<%= value.url %>">
<%= h.getDirectoryName({directory: value.name}) %>
</a>
<% } %>

View File

@ -27,7 +27,7 @@
</requestFiltering>
</security>
<httpErrors errorMode="Detailed" />
<urlCompression doDynamicCompression="false" />
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<staticContent>
<mimeMap fileExtension=".md" mimeType="text/markdown" />
</staticContent>