diff --git a/.gitignore b/.gitignore index 19708b5d..f2b4fcd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode +up.json # local environment .nvmrc diff --git a/README.md b/README.md index 5ea6336c..2a3e52d2 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ $ HOST=https://confluency.atlassian.net CONTEXT=wiki AUTHTYPE=no node ./www/bin You can get a frontpage at the `localhost:3000/`. Or open your preferred browser and type `localhost:3000/page/:your-confluence-page-id`. The page id should be a number. +Test latest version [here](https://p4tc6mjgf2.execute-api.us-east-1.amazonaws.com/staging/). + ## Config You can override configs with environment variables. diff --git a/src/plugin.ts b/src/plugin.ts index 39c37dd7..6e117fa9 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,6 +3,8 @@ import * as _ from 'lodash'; import { Section, convertImageSrcSet, host, sanitizeImageSrc, parseParams } from './util'; +const baseUrl = process.env.BASEURL || ''; + export function mermaid(section: Section): Section { const $ = cheerio.load(section.body); const mermaids = $('.mermaid'); @@ -25,10 +27,10 @@ export function attached(req) { const img = $(el); if (img.data('linked-resource-type') !== 'attachment') return section; const imageSrc = img.data('image-src'); - img.attr('src', req.baseUrl + '/image' + sanitizeImageSrc(imageSrc)); + img.attr('src', req.baseUrl + baseUrl + '/image' + sanitizeImageSrc(imageSrc)); const imageSrcSet = img.attr('srcset'); if (imageSrcSet) { - img.attr('srcset', convertImageSrcSet(req.baseUrl, imageSrcSet)); + img.attr('srcset', convertImageSrcSet(req.baseUrl + baseUrl, imageSrcSet)); } }); section.body = $.html(); @@ -37,10 +39,10 @@ export function attached(req) { } function hostToAbsolute(req) { - if (req.baseUrl) return req.baseUrl; + if (req.baseUrl) return req.baseUrl + baseUrl; const hostFromHeaders: string = _.get(req, 'headers.host'); - if (hostFromHeaders.startsWith('http://')) return hostFromHeaders; - return 'http://' + hostFromHeaders; + if (hostFromHeaders.startsWith('http://')) return hostFromHeaders + baseUrl; + return 'http://' + hostFromHeaders + baseUrl; } export function backgroundImage(req) { @@ -75,7 +77,7 @@ export function emoticon(req) { imgs.map((i, el) => { const img = $(el); const imageSrc = img.attr('src'); - img.attr('src', req.baseUrl + '/emoticon' + sanitizeImageSrc(imageSrc)); + img.attr('src', req.baseUrl + baseUrl + '/emoticon' + sanitizeImageSrc(imageSrc)); img.css('border', '0px'); img.css('height', '32px'); img.css('margin', '5px'); @@ -95,10 +97,10 @@ export function gliffy(req) { const img = $(el); if (img.attr('class').trim() !== 'gliffy-image') return section; const imageSrc = img.attr('src'); - img.attr('src', req.baseUrl + '/image' + sanitizeImageSrc(imageSrc)); + img.attr('src', req.baseUrl + baseUrl + '/image' + sanitizeImageSrc(imageSrc)); const imageSrcSet = img.attr('srcset'); if (imageSrcSet) { - img.attr('srcset', convertImageSrcSet(req.baseUrl, imageSrcSet)); + img.attr('srcset', convertImageSrcSet(req.baseUrl + baseUrl, imageSrcSet)); } }); section.body = $.html(); diff --git a/src/routes/index.ts b/src/routes/index.ts index 7c0d0d16..ae34194d 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -10,6 +10,7 @@ const context = process.env.CONTEXT; const username = process.env.USERNAME; const password = process.env.PASSWORD; const authType = process.env.AUTHTYPE || 'basic'; +const baseUrl = process.env.BASEURL || ''; const pinnedPages = splitPinnedPages(process.env.PINNED_PAGES); if (!(authType === 'cookie' || authType === 'basic' || authType === 'no')) { @@ -42,7 +43,7 @@ router.get('/', (_req, res) => { confluency.search('label=miniseminar').then(data => _.map(data, pickSummary)) ]; return Promise.all(p).then(([pinned, labeled]) => { - res.render('index', { pinned, recentlyViewed, labeled, themes, transitions }); + res.render('index', { pinned, recentlyViewed, labeled, themes, transitions, baseUrl }); }); }); @@ -79,24 +80,26 @@ router.get('/page/:id', (req, res, next) => { section.sections = section.sections.map(section => map(section)); return section; }); - res.render('page', { title: page.title, req, sections, theme, transition, printPdf }); + res.render('page', { title: page.title, req, sections, theme, transition, printPdf, baseUrl }); }).catch(next); }); -router.get(/\/image\/(.*)/, (req, res, next) => { +router.get(/\/image\/(.*)/, (req, res) => { const uri = `/${encodeURI(req.params[0])}?${querystring.stringify(req.query)}`; return confluency.newRequest('get', uri, true).pipe(res); }); -router.get(/\/emoticon\/(.*)/, (req, res, next) => { +router.get(/\/emoticon\/(.*)/, (req, res) => { const uri = `/${encodeURI(req.params[0])}?${querystring.stringify(req.query)}`; return confluency.newRequest('get', uri, true).pipe(res); }); -router.get('/gist/:userId/:gistId', (req, res, next) => { - res.render('md', { url: `https://gist.githubusercontent.com/${req.params.userId}/${req.params.gistId}/raw` }); +router.get('/gist/:userId/:gistId', (req, res) => { + const url = `https://gist.githubusercontent.com/${req.params.userId}/${req.params.gistId}/raw`; + res.render('md', { url, baseUrl }); }); -router.get('/gist/:userId/:gistId/:commitHash', (req, res, next) => { - res.render('md', { url: `https://gist.githubusercontent.com/${req.params.userId}/${req.params.gistId}/raw/${req.params.commitHash}` }); +router.get('/gist/:userId/:gistId/:commitHash', (req, res) => { + const url = `https://gist.githubusercontent.com/${req.params.userId}/${req.params.gistId}/raw/${req.params.commitHash}`; + res.render('md', { url, baseUrl }); }); diff --git a/views/index.pug b/views/index.pug index a46d1952..e710acaa 100644 --- a/views/index.pug +++ b/views/index.pug @@ -22,19 +22,19 @@ block content ul each i in pinned li - a(href=`/page/${i.id}`)= i.title + a(href=`${baseUrl}/page/${i.id}`)= i.title h1 Recently viewed ul each i in recentlyViewed li - a(href=`/page/${i.id}`)= i.title + a(href=`${baseUrl}/page/${i.id}`)= i.title h1 Labeled as #miniseminar ul each i in labeled li - a(href=`/page/${i.id}`)= i.title + a(href=`${baseUrl}/page/${i.id}`)= i.title div.division p Query: diff --git a/views/md.pug b/views/md.pug index 8793fee0..31b0f96b 100644 --- a/views/md.pug +++ b/views/md.pug @@ -2,9 +2,9 @@ doctype html html head title= title - link(rel='stylesheet', href=`/css/reveal.css`) - link(rel='stylesheet', href=`/css/theme/${theme || 'black'}.css`)#theme - link(rel='stylesheet', href=`/lib/css/zenburn.css`) + link(rel='stylesheet', href=`${baseUrl}/css/reveal.css`) + link(rel='stylesheet', href=`${baseUrl}/css/theme/${theme || 'black'}.css`)#theme + link(rel='stylesheet', href=`${baseUrl}/lib/css/zenburn.css`) link(rel='stylesheet', href='https://cdn.rawgit.com/knsv/mermaid/7.0.0/dist/mermaid.css') script(src='https://cdn.rawgit.com/knsv/mermaid/7.0.0/dist/mermaid.min.js') script(src='https://cdn.rawgit.com/mathiasbynens/he/670991a4/he.js') @@ -13,7 +13,7 @@ html var link = document.createElement( 'link' ); link.rel = 'stylesheet'; link.type = 'text/css'; - link.href = window.location.search.match( /print-pdf/gi ) ? '/css/print/pdf.css' : '/css/print/paper.css'; + link.href = window.location.search.match( /print-pdf/gi ) ? `${baseUrl}/css/print/pdf.css` : `${baseUrl}/css/print/paper.css`; document.getElementsByTagName( 'head' )[0].appendChild( link ); body @@ -21,8 +21,7 @@ html .slides section(data-markdown=url) - script(src=`/lib/js/head.min.js`) - script(src=`/js/reveal.js`) + script(src=`${baseUrl}/js/reveal.js`) script. // https://github.com/hakimel/reveal.js#configuration Reveal.initialize({ @@ -35,12 +34,12 @@ html // Optional reveal.js plugins dependencies: [ - { src: `/plugin/markdown/marked.js`, async: true }, - { src: `/plugin/markdown/markdown.js`, async: true }, - { src: `/lib/js/classList.js`, condition: function() { return !document.body.classList; } }, - { src: `/plugin/highlight/highlight.js`, async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, - { src: `/plugin/zoom-js/zoom.js`, async: true }, - { src: `/plugin/notes/notes.js`, async: true } + { src: `#{baseUrl}/plugin/markdown/marked.js`, async: true }, + { src: `#{baseUrl}/plugin/markdown/markdown.js`, async: true }, + { src: `#{baseUrl}/lib/js/classList.js`, condition: function() { return !document.body.classList; } }, + { src: `#{baseUrl}/plugin/highlight/highlight.js`, async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, + { src: `#{baseUrl}/plugin/zoom-js/zoom.js`, async: true }, + { src: `#{baseUrl}/plugin/notes/notes.js`, async: true } ] }); var nextId = 0; diff --git a/views/page.pug b/views/page.pug index 5d8d0cce..f96d51ee 100644 --- a/views/page.pug +++ b/views/page.pug @@ -2,9 +2,9 @@ doctype html html head title= title - link(rel='stylesheet', href=`/css/reveal.css`) - link(rel='stylesheet', href=`/css/theme/${theme || 'black'}.css`)#theme - link(rel='stylesheet', href=`/lib/css/zenburn.css`) + link(rel='stylesheet', href=`${baseUrl}/css/reveal.css`) + link(rel='stylesheet', href=`${baseUrl}/css/theme/${theme || 'black'}.css`)#theme + link(rel='stylesheet', href=`${baseUrl}/lib/css/zenburn.css`) link(rel='stylesheet', href='https://cdn.rawgit.com/knsv/mermaid/7.0.0/dist/mermaid.css') script(src='https://cdn.rawgit.com/knsv/mermaid/7.0.0/dist/mermaid.min.js') script(src='https://cdn.rawgit.com/mathiasbynens/he/670991a4/he.js') @@ -13,7 +13,7 @@ html var link = document.createElement( 'link' ); link.rel = 'stylesheet'; link.type = 'text/css'; - link.href = window.location.search.match( /print-pdf/gi ) ? '/css/print/pdf.css' : '/css/print/paper.css'; + link.href = window.location.search.match( /print-pdf/gi ) ? `${baseUrl}/css/print/pdf.css` : `${baseUrl}/css/print/paper.css`; document.getElementsByTagName( 'head' )[0].appendChild( link ); body @@ -27,8 +27,7 @@ html each subSection in section.sections section(data-background=subSection.background)!=subSection.body - script(src=`/lib/js/head.min.js`) - script(src=`/js/reveal.js`) + script(src=`${baseUrl}/js/reveal.js`) script. // https://github.com/hakimel/reveal.js#configuration Reveal.initialize({ @@ -41,10 +40,10 @@ html // Optional reveal.js plugins dependencies: [ - { src: `/lib/js/classList.js`, condition: function() { return !document.body.classList; } }, - { src: `/plugin/highlight/highlight.js`, async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, - { src: `/plugin/zoom-js/zoom.js`, async: true }, - { src: `/plugin/notes/notes.js`, async: true } + { src: `#{baseUrl}/lib/js/classList.js`, condition: function() { return !document.body.classList; } }, + { src: `#{baseUrl}/plugin/highlight/highlight.js`, async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, + { src: `#{baseUrl}/plugin/zoom-js/zoom.js`, async: true }, + { src: `#{baseUrl}/plugin/notes/notes.js`, async: true } ] }); var nextId = 0;