Skip to content

Commit

Permalink
feat: support apex/up (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
heycalmdown authored Dec 27, 2019
1 parent fe48519 commit 3f15b44
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
up.json

# local environment
.nvmrc
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 10 additions & 8 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand Down
19 changes: 11 additions & 8 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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 });
});
});

Expand Down Expand Up @@ -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 });
});
6 changes: 3 additions & 3 deletions views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 11 additions & 12 deletions views/md.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -13,16 +13,15 @@ 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
.reveal
.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({
Expand All @@ -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;
Expand Down
19 changes: 9 additions & 10 deletions views/page.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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({
Expand All @@ -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;
Expand Down

0 comments on commit 3f15b44

Please sign in to comment.