Skip to content

Commit

Permalink
fix(code): syntax highlight doesn't work from 5.9 (#16)
Browse files Browse the repository at this point in the history
* support 5.8 and 5.9 altogether
* help hljs to detect the languge well

fix #2
  • Loading branch information
heycalmdown authored Mar 3, 2017
1 parent b2f5157 commit 63b57e0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"codefactor",
"app",
"img",
"code",
"home"
],
"validate": true
Expand Down
57 changes: 50 additions & 7 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as superagent from 'superagent';
import * as url from 'url';
import * as _ from 'lodash';

import { host, sanitizeImageSrc, splitPinnedPages } from '../util';
import { host, sanitizeImageSrc, splitPinnedPages, parseParams } from '../util';

const context = process.env.CONTEXT;
const username = process.env.USERNAME;
Expand Down Expand Up @@ -75,19 +75,62 @@ function link(section) {
});
return $.html();
}
function code(section) {
const $ = cheerio.load(section, {xmlMode: true});
const script = $('.code.panel.pdl script[type=syntaxhighlighter]');
if (script.length === 0) return section;

const LANGS = {
actionscript3: 'lang-actionscript',
'c#': 'lang-cs',
coldfusion: 'lang-xx',
jfx: 'lang-java',
jscript: 'lang-js',
text: 'lang-md',
powershell: 'lang-powershell',
sass: 'lang-scss'
};

function brushToLang(brush) {
return LANGS[brush] || 'lang-' + brush;
}

function codeFor58(script) {
let code = 'nocontent';
try {
code = script[0].children[0].children[0].data;
} catch (e) {
console.error(e);
}
script.parent().html(`<pre><code data-trim data-noescape class="lang-javascript" style="font-size: smaller">${code}</code></pre>`);
return $.html();
const params = parseParams(pre.data('syntaxhighlighter-params'));
const c = brushToLang(params.brush);
const s = 'font-size: smaller';
script.parent().html(`<pre><code data-trim data-noescape class="${c}" style="${s}">${code}</code></pre>`);
}

function codeFor59(pre) {
const code = pre[0].children[0].data;
const params = parseParams(pre.data('syntaxhighlighter-params'));
const c = brushToLang(params.brush);
const s = 'font-size: smaller';
pre.parent().html(`<pre><code data-trim data-noescape class="${c}" style="${s}">${code}</code></pre>`)
}

function code(section) {
const $ = cheerio.load(section, {xmlMode: true});

// for confluence-5.8
const script = $('.code.panel.pdl script[type=syntaxhighlighter]');
if (script.length !== 0) {
codeFor58(script);
return $.html();
}

// for confluence-5.9
const pre = $('.codeContent.panelContent.pdl pre');
if (pre.length !== 0) {
codeFor59(pre);
return $.html();
}
return section;
}

function fragment(section) {
const $ = cheerio.load(section);
const liList = $('li');
Expand Down
8 changes: 8 additions & 0 deletions src/spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ describe('miniseminar', () => {
expect(splitPinnedPages('')).toEqual([]);
expect(splitPinnedPages()).toEqual([]);
});
it('should parse params', () => {
expect(parseParams('brush: js; gutter: false; theme: Confluence'))
.toEqual({
brush: 'js',
gutter: 'false',
theme: 'Confluence'
});
})
});
11 changes: 11 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require('lodash');

export let host = process.env.HOST;

export function setHost(h) {
Expand All @@ -13,3 +15,12 @@ export function sanitizeImageSrc(imageSrc) {
if (!imageSrc.startsWith(host)) return imageSrc;
return imageSrc.slice(host.length);
}

export function parseParams(params) {
return _.merge(...params.split(';').map(param => {
const [key, value] = param.split(':');
return {
[key.trim()]: value.trim()
};
}));
}

0 comments on commit 63b57e0

Please sign in to comment.