Skip to content

Commit

Permalink
fix: multiple codeblock failure
Browse files Browse the repository at this point in the history
  • Loading branch information
heycalmdown committed Aug 29, 2018
1 parent b1841ac commit 1d9e828
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
41 changes: 23 additions & 18 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,32 @@ const LANGS = {
sass: 'lang-scss'
};

function brushToLang(brush) {
function brushToLang(brush: string) {
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);
}
const s = 'font-size: smaller';
script.parent().html(`<pre><code data-trim data-noescape style="${s}">${code}</code></pre>`);
function codeFor58($: CheerioStatic, scripts: Cheerio) {
scripts.map((_i, elem) => {
const script = $(elem);
try {
const code = elem.children[0].children[0].data || 'nocontent';
const s = 'font-size: smaller';
script.parent().html(`<pre><code data-trim data-noescape style="${s}">${code}</code></pre>`);
} catch (e) {
console.error(e);
}
});
}

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 codeFor59($: CheerioStatic, pres: Cheerio) {
pres.map((_i, elem) => {
const pre = $(elem);
const code = elem.children[0].data || 'nocontent';
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>`)
});
}

export function code(section: Section): Section {
Expand All @@ -142,15 +147,15 @@ export function code(section: Section): Section {
// for confluence-5.8
const script = $('.code.panel.pdl script[type=syntaxhighlighter]');
if (script.length !== 0) {
codeFor58(script);
codeFor58($, script);
section.body = $.html();
return section;
}

// for confluence-5.9
const pre = $('.codeContent.panelContent.pdl pre');
if (pre.length !== 0) {
codeFor59(pre);
codeFor59($, pre);
section.body = $.html();
return section;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function sanitizeImageSrc(imageSrc) {
return imageSrc;
}

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

0 comments on commit 1d9e828

Please sign in to comment.