Skip to content

Commit

Permalink
Fixed issues with code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pastorsj committed Feb 17, 2018
1 parent 30a253f commit 1475283
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-gist-html",
"version": "1.1.3",
"version": "1.1.4",
"description": "A simple library to convert gist code and GitHub files into gist-like html",
"main": "./lib/node-gist-html.min.js",
"types": "./index.d.ts",
Expand Down
13 changes: 13 additions & 0 deletions src/converters/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ function removeLines($, lineNumbers) {
}
}

function formatCode($) {
// eslint-disable-next-line
$('td.blob-code').each(function(index, element) {
const outer = $.html(this);
const inner = $(this).html();
const replacement = `<pre><code>${$(this).html()}</code></pre>`;
const replaced = outer.replace(inner, replacement);
$(this).replaceWith($(replaced));
});
}

function retrieveGist(response, options) {
return new Promise((resolve, reject) => {
try {
if (response && response.div) {
if (response.stylesheet) {
if (response.stylesheet.indexOf('<link') === 0) {
// eslint-disable-next-line
response.stylesheet = response.stylesheet
.replace(/\\/g, '')
.match(/href=\"([^\s]*)\"/)[1];
Expand All @@ -51,6 +63,7 @@ function retrieveGist(response, options) {
if (options.lineNumbers) {
removeLines($, options.lineNumbers);
}
formatCode($);

const file = $.html();

Expand Down
13 changes: 13 additions & 0 deletions src/converters/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ function removeLines($, lineNumbers) {
}
}

function formatCode($) {
// eslint-disable-next-line
$('td.blob-code').each(function(index, element) {
const outer = $.html(this);
const inner = $(this).html();
const replacement = `<pre><code>${$(this).html()}</code></pre>`;
const replaced = outer.replace(inner, replacement);
$(this).replaceWith($(replaced));
});
}

function convertGithubCode(body, url, filename, options) {
return new Promise((resolve, reject) => {
try {
Expand All @@ -40,8 +51,10 @@ function convertGithubCode(body, url, filename, options) {
removeLines($, options.lineNumbers);
}

formatCode($);
let file = $('.file').html();

// eslint-disable-next-line
$('link[rel=stylesheet]').each(function (index, element) {
const href = $(this).attr('href');
if (href.indexOf('frameworks-') === -1) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const fs = require('fs');
// console.error(err);
// });

converter.gistify('https://github.com/pastorsj/blog-api/blob/master/docs/index.html#L2-L18')
converter.gistify('https://github.com/LighthouseBlog/Blog/blob/master/src/app/_interceptors/response.interceptor.ts')
.then((result) => {
fs.writeFileSync('index.html', result.html);
})
Expand Down

0 comments on commit 1475283

Please sign in to comment.