forked from nm3clol/nm3clol-express-app
fix: Fixed side effects from hardcoded paths.
This commit is contained in:
parent
5f4ac46af7
commit
9cb16680f9
|
@ -3,12 +3,13 @@ console.log(`Configuring .env and expanding .env to include environment variable
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const dotenv = require('dotenv');
|
const dotenv = require('dotenv');
|
||||||
const dotenvExpand = require('dotenv-expand');
|
const dotenvExpand = require('dotenv-expand');
|
||||||
|
const process = require('process');
|
||||||
|
|
||||||
let env = dotenv.config();
|
let env = dotenv.config();
|
||||||
dotenvExpand.expand(env);
|
dotenvExpand.expand(env);
|
||||||
|
|
||||||
const getAppHttpHost = () => env.APP_HTTP_HOST||'nm3clol-express-app';
|
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 = () => {
|
const getAppHttpUrl = () => {
|
||||||
return env.APP_HTTP_URL || `http://${getAppHttpHost() + ((getAppHttpPort() === 80) ? '' : ':' + getAppHttpPort())}`
|
return env.APP_HTTP_URL || `http://${getAppHttpHost() + ((getAppHttpPort() === 80) ? '' : ':' + getAppHttpPort())}`
|
||||||
};
|
};
|
||||||
|
@ -31,19 +32,23 @@ const getSolrLawCore = () => env.SOLR_LAW_CORE||'vacode_core';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
config: {
|
config: {
|
||||||
publicPath: getPublicPath(),
|
appHttpHost: getAppHttpHost(),
|
||||||
pagesPath: getPagesPath(),
|
appHttpPort: getAppHttpPort(),
|
||||||
staticPath: getStaticPath(),
|
appHttpUrl: getAppHttpUrl(),
|
||||||
|
|
||||||
siteName: getSiteName(),
|
siteName: getSiteName(),
|
||||||
siteWelcomeMessage: getSiteWelcomeMessage(),
|
siteWelcomeMessage: getSiteWelcomeMessage(),
|
||||||
siteHost: getSiteHost(),
|
siteHost: getSiteHost(),
|
||||||
siteUrl: getSiteUrl(),
|
siteUrl: getSiteUrl(),
|
||||||
appHttpHost: getAppHttpHost(),
|
|
||||||
appHttpPort: getAppHttpPort(),
|
publicPath: getPublicPath(),
|
||||||
appHttpUrl: getAppHttpUrl(),
|
pagesPath: getPagesPath(),
|
||||||
|
staticPath: getStaticPath(),
|
||||||
|
|
||||||
solrDocsHost: getSolrDocsHost(),
|
solrDocsHost: getSolrDocsHost(),
|
||||||
solrDocsPort: getSolrDocsPort(),
|
solrDocsPort: getSolrDocsPort(),
|
||||||
solrDocsCore: getSolrDocsCore(),
|
solrDocsCore: getSolrDocsCore(),
|
||||||
|
|
||||||
solrLawHost: getSolrLawHost(),
|
solrLawHost: getSolrLawHost(),
|
||||||
solrLawPort: getSolrLawPort(),
|
solrLawPort: getSolrLawPort(),
|
||||||
solrLawCore: getSolrLawCore(),
|
solrLawCore: getSolrLawCore(),
|
||||||
|
|
|
@ -88,7 +88,7 @@ glob.globSync('**/*.md', {
|
||||||
const fmData = { fm: fm.data, excerpt: fm.excerpt };
|
const fmData = { fm: fm.data, excerpt: fm.excerpt };
|
||||||
const content = helpers.md.render(fm.content, fmData );
|
const content = helpers.md.render(fm.content, fmData );
|
||||||
const renderData = { content, route, filePath, fullFilePath, req, paths, ...fmData };
|
const renderData = { content, route, filePath, fullFilePath, req, paths, ...fmData };
|
||||||
res.render("page", { h: helpers, ...renderData });
|
res.render("page", { h: helpers, require, ...renderData });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ const helpers = require('../views/helpers/functions');
|
||||||
// const errorTemplate = require('./views/error');
|
// const errorTemplate = require('./views/error');
|
||||||
const directoryTemplate = (vals) => {
|
const directoryTemplate = (vals) => {
|
||||||
return new Promise((resolve, reject) => {
|
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) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
@ -36,7 +36,7 @@ const directoryTemplate = (vals) => {
|
||||||
};
|
};
|
||||||
const errorTemplate = (vals) => {
|
const errorTemplate = (vals) => {
|
||||||
return new Promise((resolve, reject) => {
|
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) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -22,7 +22,9 @@
|
||||||
<i> </i>
|
<i> </i>
|
||||||
<%= h.getSiteWelcomeMessage() %>
|
<%= h.getSiteWelcomeMessage() %>
|
||||||
<% } else if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
|
<% } 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 { %>
|
<% } else { %>
|
||||||
<a href="/<%= value.url %>">
|
<a href="/<%= value.url %>">
|
||||||
<%= h.getDirectoryName({directory: value.name}).replaceAll('_', ' ') %>
|
<%= h.getDirectoryName({directory: value.name}).replaceAll('_', ' ') %>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const glob = require('glob');
|
const glob = require('glob');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const config = require('../../app/config');
|
const { config } = require('../../app/config');
|
||||||
const markdownit = require('markdown-it');
|
const markdownit = require('markdown-it');
|
||||||
var markdownItAttrs = require('markdown-it-attrs');
|
var markdownItAttrs = require('markdown-it-attrs');
|
||||||
const md = markdownit({
|
const md = markdownit({
|
||||||
|
@ -18,34 +18,50 @@ const md = markdownit({
|
||||||
);
|
);
|
||||||
const moment = require('moment-timezone').tz.setDefault("UTC");
|
const moment = require('moment-timezone').tz.setDefault("UTC");
|
||||||
|
|
||||||
const getSiteName = config.getSiteName;
|
const getSiteName = () => config.siteName;
|
||||||
|
|
||||||
const trimSlashes = ({path}) => {
|
const trimSlashes = (dirPath) => {
|
||||||
return path.replace(/^[\/\\]|[\/\\]$/g, '');
|
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}) => {
|
const getDirectoryName = ({directory}) => {
|
||||||
directory = trimSlashes({path: directory});
|
directory = trimSlashes(directory);
|
||||||
let title = trimSlashes({path: directory.replace("public", "")}).replaceAll(path.sep, path.posix.sep);
|
const leftMostDirectory = getLeftMostDirectory(directory);
|
||||||
return (directory=="public") ? getSiteName() : title;
|
let title = trimSlashes(leftTrimFirstDirectory(directory)).replaceAll(path.sep, path.posix.sep);
|
||||||
|
return (trimSlashes(directory)==leftMostDirectory) ? getSiteName() : title;
|
||||||
};
|
};
|
||||||
const getDirectoryTitle = ({directory}) => {
|
const getDirectoryTitle = ({directory}) => {
|
||||||
directory = trimSlashes({path: directory});
|
const leftMostDirectory = getLeftMostDirectory(directory);
|
||||||
let title = trimSlashes({path: directory.replace("public", "")})
|
let title = trimSlashes(leftTrimFirstDirectory(directory))
|
||||||
.replaceAll(path.sep, path.posix.sep)
|
.replaceAll(path.sep, path.posix.sep)
|
||||||
.replaceAll('_', ' ')
|
.replaceAll('_', ' ')
|
||||||
.split('/')
|
.split('/')
|
||||||
.reverse()
|
.reverse()
|
||||||
.join(' - ');
|
.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 shouldShowDirectorySeparator = ({index}) => (index > 0);
|
||||||
const shouldShowSiteWelcomeMessage = ({paths}) => (paths.length == 1);
|
const shouldShowSiteWelcomeMessage = ({paths}) => (paths.length == 1);
|
||||||
const shouldOmitLinkOnLastBreadcrumb = ({paths, index}) => (index == paths.length-1);
|
const shouldOmitLinkOnLastBreadcrumb = ({paths, index}) => (index == paths.length-1);
|
||||||
|
|
||||||
const resolveReadmeFile = ({directory}) => {
|
const resolveReadmeFile = ({directory}) => {
|
||||||
const resolveFile = (file) => {
|
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 fs.existsSync(pathToFile) ? pathToFile : "";
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
|
@ -157,6 +173,7 @@ const renderArchive = (html, paths) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
leftTrimFirstDirectory,
|
||||||
trimSlashes,
|
trimSlashes,
|
||||||
getSiteName,
|
getSiteName,
|
||||||
getDirectoryName,
|
getDirectoryName,
|
||||||
|
|
|
@ -20,8 +20,10 @@
|
||||||
<% } %>
|
<% } %>
|
||||||
<% if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
|
<% if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
|
||||||
<%= (typeof fm.title !== 'undefined') ? `${fm.title}` : value %>
|
<%= (typeof fm.title !== 'undefined') ? `${fm.title}` : value %>
|
||||||
|
<% } else if (index == 0) { %>
|
||||||
|
<a href="/"><%= h.getSiteName() %></a>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<a href="/<%= value.replace('public', '') != '' ? value.replace('public', '') : '' %>">
|
<a href="/<%= h.trimSlashes(h.leftTrimFirstDirectory(value)) %>">
|
||||||
<%= (value == 'public' ? h.getSiteName() : value) %>
|
<%= (value == 'public' ? h.getSiteName() : value) %>
|
||||||
</a>
|
</a>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
Welcome to <%= h.getDirectoryTitle({directory}) %>
|
Welcome to <%= h.getDirectoryTitle({directory}) %>
|
||||||
<% } else if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
|
<% } else if (h.shouldOmitLinkOnLastBreadcrumb({paths, index})) { %>
|
||||||
<%= h.trimSlashes({path: value.name}) %>
|
<%= h.trimSlashes({path: value.name}) %>
|
||||||
|
<% } else if (index == 0) { %>
|
||||||
|
<a href="/"><%= h.getSiteName() %></a>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<a href="/<%= value.url %>">
|
<a href="/<%= value.url %>">
|
||||||
<%= h.getDirectoryName({directory: value.name}) %>
|
<%= h.getDirectoryName({directory: value.name}) %>
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
</requestFiltering>
|
</requestFiltering>
|
||||||
</security>
|
</security>
|
||||||
<httpErrors errorMode="Detailed" />
|
<httpErrors errorMode="Detailed" />
|
||||||
<urlCompression doDynamicCompression="false" />
|
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
|
||||||
<staticContent>
|
<staticContent>
|
||||||
<mimeMap fileExtension=".md" mimeType="text/markdown" />
|
<mimeMap fileExtension=".md" mimeType="text/markdown" />
|
||||||
</staticContent>
|
</staticContent>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user