Skip to content

Commit d56a8cd

Browse files
authored
Merge pull request #58 from peopledoc/feat/beautify-html-examples-with-prettier
Feat/beautify html examples with prettier
2 parents 5f6f15e + 22a500a commit d56a8cd

8 files changed

+90
-51
lines changed

assets/styledown.css

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ code.sg {
136136
*/
137137

138138
.sg-code {
139+
max-height: 20vw;
140+
139141
border: solid 1px transparent;
140142
overflow-x: auto;
141143
border-bottom-left-radius: 3px;

index.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const Cheerio = require('cheerio')
99
const extend = require('util')._extend
1010
const mdextract = require('mdextract')
1111
const hljs = require('highlight.js')
12-
const beautify = require('js-beautify').html_beautify
1312
const { version } = require('./package.json')
1413
const { readFileSync } = require('fs')
1514
const default_conf = require('./lib/default_conf.js')
15+
const { beautifyHTML } = require('./lib/utils')
1616

1717
const {
1818
addClasses,
@@ -175,16 +175,9 @@ class Styledown {
175175

176176
prettyprint(html, options) {
177177

178-
let opts = {
179-
indent_size: this.options.indentSize,
180-
wrap_line_length: 120,
181-
unformatted: ['pre']
182-
}
183-
// js-beautify sometimes trips when the first character isn't a <. not
184-
// sure... but might as well do this.
185-
html = html.trim()
178+
let output = html.trim()
186179

187-
let output = beautify(html, extend(opts, options))
180+
output = beautifyHTML(html, options)
188181

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

lib/utils.js

+63-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Pug = require('pug')
2+
const prettier = require('prettier')
23

34
/**
45
* Get the tags and code out of the code text.
@@ -10,45 +11,53 @@ const Pug = require('pug')
1011
* => { tag: null, code: 'hello' }
1112
*/
1213

13-
exports.parseCodeText = function (code) {
14-
let m = code.trim().match(/^@([^\n]+)/)
14+
function parseCodeText(code) {
15+
let matches = code.trim().match(/^@([^\n]+)/)
1516

16-
if (m) return { tag: m[1], code: code.substr(m[1].length+2) }
17-
return { tag: null, code }
17+
if (matches) {
18+
return {
19+
tag: matches[1],
20+
code: code.substr(matches[1].length+2)
21+
}
22+
}
23+
return {
24+
tag: null,
25+
code
26+
}
1827
}
1928

2029
/**
2130
* Parse tags
2231
*/
2332

24-
exports.parseTags = function (str) {
33+
function parseTags(str) {
2534
if (typeof str !== 'string') return {}
2635

27-
let m
36+
let matches
2837
let obj = {}
2938
str = str.trim()
3039

3140
/* eslint-disable */
3241
while (true) {
33-
if (m = str.match(/^\.([a-z\-]+)\s*/)) {
34-
if (!obj["class"]) obj["class"] = []
35-
obj["class"].push(m[1])
36-
} else if (m = str.match(/^([a-z\-]+)="([^"]+)"\s*/)) {
37-
obj[m[1]] = m[2]
38-
} else if (m = str.match(/^([a-z\-]+)='([^']+)'\s*/)) {
39-
obj[m[1]] = m[2]
40-
} else if (m = str.match(/^([a-z\-]+)=([^\s]+)\s*/)) {
41-
obj[m[1]] = m[2]
42-
} else if (m = str.match(/^([a-z\-]+)\s*/)) {
43-
obj[m[1]] = true
42+
if (matches = str.match(/^\.([a-z\-]+)\s*/)) {
43+
if (!obj['class']) obj['class'] = []
44+
obj['class'].push(matches[1])
45+
} else if (matches = str.match(/^([a-z\-]+)="([^"]+)"\s*/)) {
46+
obj[matches[1]] = matches[2]
47+
} else if (matches = str.match(/^([a-z\-]+)='([^']+)'\s*/)) {
48+
obj[matches[1]] = matches[2]
49+
} else if (matches = str.match(/^([a-z\-]+)=([^\s]+)\s*/)) {
50+
obj[matches[1]] = matches[2]
51+
} else if (matches = str.match(/^([a-z\-]+)\s*/)) {
52+
obj[matches[1]] = true
4453
} else {
45-
if (obj["class"]) obj["class"] = obj["class"].join(' ')
54+
if (obj['class']) obj['class'] = obj['class'].join(' ')
4655
return obj
4756
}
4857
/* eslint-enable */
4958

5059
// Trim
51-
str = str.substr(m[0].length)
60+
str = str.substr(matches[0].length)
5261
}
5362
}
5463

@@ -59,7 +68,7 @@ exports.parseTags = function (str) {
5968
* prefixClass('pad dark', 'sg') => 'sg-pad sg-dark'
6069
*/
6170

62-
exports.prefixClass = function (klass, prefix) {
71+
function prefixClass(klass, prefix) {
6372
return klass.split(' ').map(function (n) {
6473
return n.length > 0 ? (`${prefix }-${ n}`) : n
6574
}).join(' ')
@@ -68,8 +77,7 @@ exports.prefixClass = function (klass, prefix) {
6877
/**
6978
* Processes a block of text as either HTML or Pug.
7079
*/
71-
72-
exports.htmlize = function (src, filePath = '.') {
80+
function htmlize(src, filePath = '.') {
7381
// Mdconf processes them as arrays
7482
if (src.constructor === Array) {
7583
/* eslint-disable-next-line prefer-destructuring */
@@ -79,9 +87,41 @@ exports.htmlize = function (src, filePath = '.') {
7987
if (src.substr(0, 1) === '<') {
8088
return src
8189
} else {
82-
return Pug.render(src, {
90+
let html = Pug.render(src, {
8391
filename: filePath,
8492
doctype: 'html'
8593
})
94+
return beautifyHTML(html)
8695
}
8796
}
97+
98+
/**
99+
* Beautify HTML text with prettier
100+
*
101+
* @param {string} html
102+
*/
103+
function beautifyHTML(html, options) {
104+
let output = prettier.format(html, {
105+
parser: 'html',
106+
htmlWhitespaceSensitivity: 'ignore',
107+
insertPragma: false,
108+
printWidth: 120,
109+
proseWrap: 'preserve',
110+
requirePragma: false,
111+
singleQuote: false,
112+
tabWidth: 2,
113+
useTabs: false,
114+
vueIndentScriptAndStyle: false,
115+
...options
116+
})
117+
118+
return output.replace(/\/>/g, '>') // Prettier add trailling slash to self-closing elements but we don't want them
119+
}
120+
121+
module.exports = {
122+
parseCodeText,
123+
parseTags,
124+
prefixClass,
125+
htmlize,
126+
beautifyHTML
127+
}

package-lock.json

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"marked": "^0.3.0",
2424
"mdextract": "^1.0.0",
2525
"minimist": "^1.2.0",
26+
"prettier": "^2.1.2",
2627
"pug": "^3.0.0",
2728
"read-input": "^0.3.0"
2829
},

test/markdown.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Markdown', function() {
2323
expect(this.$).have.selector('p.sg')
2424
})
2525
it('html template', function() {
26-
expect(this.html).match(/doctype html/)
26+
expect(this.html).match(/!DOCTYPE html/)
2727
expect(this.html).match(/body/)
2828
expect(this.html).match(/head/)
2929
expect(this.$).have.selector('meta[charset="utf-8"]')

test/pre_tag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('Pre tag', function() {
8484
expect(this.$).have.selector('pre')
8585
})
8686
it('makes a <pre> HTML code', function() {
87-
expect(this.$("pre").text()).eql('<div class="button"></div>')
87+
expect(this.$("pre").text()).eql('<div class="button"></div>\n')
8888
})
8989
})
9090
})

test/pretty_print.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,37 @@ const setupTestEnv = require('./setup')
22

33
describe('Pretty Print', function() {
44
setupTestEnv(this)
5-
5+
66
describe('default', function() {
77
beforeEach(function() {
88
this.load("### Hello\n\n @example\n div", {
99
head: ''
1010
})
1111
})
12-
it('not indent <head>', function() {
13-
expect(this.html).match(/\n<head/)
12+
it('indent <head>', function() {
13+
expect(this.html).match(/\n {2}<head/)
1414
})
15-
it('not indent <body>', function() {
16-
expect(this.html).match(/\n<body/)
15+
it('indent <body>', function() {
16+
expect(this.html).match(/\n {2}<body/)
1717
})
1818
it('indent .sg-section-hello', function() {
19-
expect(this.html).match(/\n {4}<section class="sg-block sg-section-hello/)
19+
expect(this.html).match(/\n {6}<section class="sg-block sg-section-hello/)
2020
})
2121
it('indent .sg-canvas', function() {
22-
expect(this.html).match(/\n {8}<div class="sg-canvas/)
22+
expect(this.html).match(/\n {10}<div class="sg-canvas/)
2323
})
2424
})
2525
describe('custom indentSize', function() {
2626
beforeEach(function() {
2727
this.load("### Hello\n\n @example\n div", {
28-
indentSize: 4,
29-
head: ''
28+
tabWidth: 4
3029
})
3130
})
32-
it('should work', function() {})
3331
it('indent .sg-section-hello', function() {
34-
expect(this.html).match(/\n {8}<section class="sg-block sg-section-hello/)
32+
expect(this.html).match(/<section class="sg-block sg-section-hello/)
3533
})
3634
it('indent .sg-canvas', function() {
37-
expect(this.html).match(/\n {16}<div class="sg-canvas/)
35+
expect(this.html).match(/\n {8}<div class="sg-canvas/)
3836
})
3937
})
4038
})

0 commit comments

Comments
 (0)