-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from pastorsj/feature/extras
Extra Features
- Loading branch information
Showing
6 changed files
with
238 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,5 +60,5 @@ typings/ | |
lib/ | ||
tests/index.html | ||
|
||
|
||
index.html | ||
index.html | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict'; | ||
|
||
import { minify } from 'html-minifier'; | ||
import cheerio from 'cheerio'; | ||
|
||
function removeLines($, lineNumbers) { | ||
const start = lineNumbers.startLine; | ||
const end = lineNumbers.endLine; | ||
const gistName = lineNumbers.gistName; | ||
let i = 0; | ||
|
||
while (true) { | ||
i++; | ||
if (i >= start && i <= end) { | ||
continue; | ||
} | ||
const line = $(`#${gistName}L${i}`).get(); | ||
|
||
if (line.length > 0) { | ||
$(`#${gistName}L${i}`).remove(); | ||
$(`#${gistName}LC${i}`).remove(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function retrieveGist(response, options) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
if (response && response.div) { | ||
|
||
if (response.stylesheet) { | ||
|
||
if (response.stylesheet.indexOf('<link') === 0) { | ||
response.stylesheet = | ||
response.stylesheet | ||
.replace(/\\/g, '') | ||
.match(/href=\"([^\s]*)\"/)[1]; | ||
} else if (response.stylesheet.indexOf('http') !== 0) { | ||
// add leading slash if missing | ||
if (response.stylesheet.indexOf('/') !== 0) { | ||
response.stylesheet = '/' + response.stylesheet; | ||
} | ||
response.stylesheet = 'https://gist.github.com' + response.stylesheet; | ||
} | ||
} | ||
|
||
const $ = cheerio.load(response.div); | ||
|
||
if (options.removeLineNumbers) { | ||
$('.blob-num').remove(); | ||
} | ||
if (options.removeFooter) { | ||
$('.gist-meta').remove(); | ||
} | ||
if (options.lineNumbers) { | ||
removeLines($, options.lineNumbers); | ||
} | ||
|
||
const file = $.html(); | ||
|
||
const stylesheet = `<link rel=stylesheet type=text/css href=${response.stylesheet}>`; | ||
|
||
resolve({ | ||
html: minify(stylesheet + '\n' + file, { | ||
conservativeCollapse: true | ||
}), | ||
file: minify(file, { | ||
conservativeCollapse: true | ||
}), | ||
stylesheet: minify(stylesheet, { | ||
conservativeCollapse: true | ||
}) | ||
}); | ||
} else { | ||
reject('Failed to load gist'); | ||
} | ||
} catch (e) { | ||
reject('Failed to load gist'); | ||
} | ||
}); | ||
} | ||
|
||
export default retrieveGist; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
import { minify } from 'html-minifier'; | ||
import cheerio from 'cheerio'; | ||
|
||
function removeLines($, lineNumbers) { | ||
const start = lineNumbers.startLine; | ||
const end = lineNumbers.endLine; | ||
let i = 0; | ||
|
||
while (true) { | ||
i++; | ||
if (i >= start && i <= end) { | ||
continue; | ||
} | ||
const line = $(`#L${i}`).get(); | ||
|
||
if (line.length > 0) { | ||
$(`#L${i}`).remove(); | ||
$(`#LC${i}`).remove(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function convertGithubCode(body, url, filename, options) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const $ = cheerio.load(body); | ||
let styles = ''; | ||
let rawURL = $('#raw-url').attr('href'); | ||
|
||
rawURL = 'https://raw.githubusercontent.com' + rawURL.replace('/raw', ''); | ||
|
||
$('.file-header').remove(); | ||
$('.BlobToolbar').remove(); | ||
|
||
if (options.removeLineNumbers) { | ||
$('.blob-num').remove(); | ||
} | ||
if (options.lineNumbers) { | ||
removeLines($, options.lineNumbers); | ||
} | ||
|
||
let file = $('.file').html(); | ||
|
||
$('link[rel=stylesheet]').each(function (index, element) { | ||
const href = $(this).attr('href'); | ||
|
||
if (href.indexOf('frameworks-') === -1) { | ||
styles += $.html(this); | ||
} | ||
}); | ||
styles = styles + '<link rel=stylesheet type=text/css href=https://assets-cdn.github.com/assets/gist-embed-40aceec172c5b3cf62f5333920ddab3a7342a1d12dfdd1581f49f0f35fc0de4a.css>'; // eslint-disable-line | ||
|
||
if (options.removeFooter) { | ||
file = `<div class="gist"><div class="file">${file}</div></div>`; | ||
} else { | ||
let meta = ` | ||
<div class="gist-meta"> | ||
<a href="${rawURL}" style="float:right">view raw</a> | ||
<a href="${url}">${filename}</a> hosted with ❤ by <a href="https://github.com">GitHub</a> | ||
</div>`; | ||
|
||
file = `<div class="gist"><div class="file">${file}${meta}</div></div>`; | ||
} | ||
|
||
resolve({ | ||
styles: minify(styles, { | ||
conservativeCollapse: true | ||
}), | ||
file: minify(file, { | ||
conservativeCollapse: true | ||
}), | ||
html: minify(styles + file, { | ||
conservativeCollapse: true | ||
}) | ||
}); | ||
} catch(e) { | ||
reject('Failed to load github file. Please check the url'); | ||
} | ||
}); | ||
} | ||
|
||
export default convertGithubCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.