Skip to content

Commit

Permalink
fix: beautify only code-samples
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiKillertO committed Oct 2, 2020
1 parent dcd34e5 commit ffecfbf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
46 changes: 26 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ class Styledown {

html = $.html()
}
return this.prettyprint(html, { wrap_line_length: 0, ...config })
html = html.trim()

// cheerio output tends to have a bunch of extra newlines. kill them.
html = html.replace(/\n\n+/g, "\n\n")

return html
}

/**
Expand Down Expand Up @@ -139,11 +144,12 @@ class Styledown {
process(raws, options) {
return raws.map((raw) => {
let $ = Cheerio.load(raw.html)
let config = processConfig(raw.src, options)
removeConfig($)

let highlightHTML = this.highlightHTML.bind(this)
let p = this.prefix.bind(this)

let config = processConfig(raw.src, options)
removeConfig($)

// let pre = this.options.prefix

Expand All @@ -165,33 +171,32 @@ class Styledown {
})
}

/**
* prettyprint() : doc.prettyprint(html)
* (private) Reindents given `html` based on the indent size option.
*
* doc.prettyprint('<div><a>hello</a></div>')
* => "<div>\n <a>hello</a>\n</div>"
*/

prettyprint(html, options) {
// no use at the moment
// /**
// * prettyprint() : doc.prettyprint(html)
// * (private) Reindents given `html` based on the indent size option.
// *
// * doc.prettyprint('<div><a>hello</a></div>')
// * => "<div>\n <a>hello</a>\n</div>"
// */

let output = html.trim()
// prettyprint() {
// let html = this.toHTML()

output = beautifyHTML(html, options)
// let config = this.raws.reduce((finalConfig, raw) => {
// return extend(finalConfig, raw.config)
// }, this.options)

// cheerio output tends to have a bunch of extra newlines. kill them.
output = output.replace(/\n\n+/g, "\n\n")

return output
}
// return beautifyHTML(html, { config })
// }

/**
* highlightHTML():
* (private) Syntax highlighting helper
*/

highlightHTML(html) {
html = this.prettyprint(html)
html = beautifyHTML(html)
return hljs.highlight('html', html).value
}

Expand Down Expand Up @@ -248,6 +253,7 @@ Styledown.parse = function (source, options) {
return new Styledown(source, options).toHTML()
}


/**
* Styledown.version:
* The version number in semver format.
Expand Down
3 changes: 1 addition & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ function htmlize(src, filePath = '.') {
if (src.substr(0, 1) === '<') {
return src
} else {
let html = Pug.render(src, {
return Pug.render(src, {
filename: filePath,
doctype: 'html'
})
return beautifyHTML(html)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Markdown', function() {
expect(this.$).have.selector('p.sg')
})
it('html template', function() {
expect(this.html).match(/!DOCTYPE html/)
expect(this.html).match(/!doctype html/)
expect(this.html).match(/body/)
expect(this.html).match(/head/)
expect(this.$).have.selector('meta[charset="utf-8"]')
Expand Down
25 changes: 5 additions & 20 deletions test/pretty_print.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,17 @@ describe('Pretty Print', function() {
})
})

it('indent <head>', function() {
expect(this.html).match(/\n {2}<head/)
it('no indent for <head>', function() {
expect(this.html).match(/\n<head/)
})
it('indent <body>', function() {
expect(this.html).match(/\n {2}<body/)
it('no indent for <body>', function() {
expect(this.html).match(/\n<body/)
})
it('indent .sg-section-hello', function() {
expect(this.html).match(/\n {6}<section class="sg-block sg-section-hello/)
})
it('indent .sg-canvas', function() {
expect(this.html).match(/\n {10}<div class="sg-canvas/)
})
})

describe('custom indentSize', function() {
beforeEach(function() {
this.load("### Hello\n\n @example\n div", {
tabWidth: 4
})
})

it('indent .sg-section-hello', function() {
expect(this.html).match(/<section class="sg-block sg-section-hello/)
})
it('indent .sg-canvas', function() {
expect(this.html).match(/\n {8}<div class="sg-canvas/)
expect(this.html).match(/<div class="sg-canvas/)
})
})
})
Expand Down

0 comments on commit ffecfbf

Please sign in to comment.