Skip to content

Commit 57fbb88

Browse files
xiaomingplusStarpTech
authored andcommitted
feat: use tabWidth config for indent script and style tag (#71)
#59 I think the intend should be use prettier config tabWidth.
1 parent 9df234a commit 57fbb88

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

packages/prettyhtml-formatter/index.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,11 @@ function startsWithNewline(node) {
283283
function handleTemplateExpression(child, children) {
284284
if (isTemplateExpression(child.value)) {
285285
// dont touch nodes with single text element
286-
if (containsOnlyTextNodes({ children })) {
286+
if (
287+
containsOnlyTextNodes({
288+
children
289+
})
290+
) {
287291
return false
288292
}
289293

@@ -467,12 +471,21 @@ function prettierEmbeddedContent(node, level, indent, prettierOpts) {
467471
prettierOpts
468472
)
469473
)
470-
formattedText = indentPrettierOutput(formattedText, level)
474+
formattedText = indentPrettierOutput(formattedText, level, indent)
471475

472476
node.children = [
473-
{ type: 'text', value: single },
474-
{ type: 'text', value: formattedText },
475-
{ type: 'text', value: repeat(indent, level - 1) }
477+
{
478+
type: 'text',
479+
value: single
480+
},
481+
{
482+
type: 'text',
483+
value: formattedText
484+
},
485+
{
486+
type: 'text',
487+
value: repeat(indent, level - 1)
488+
}
476489
]
477490
} else if (isElement(node, 'script')) {
478491
const content = toString(node)
@@ -505,25 +518,34 @@ function prettierEmbeddedContent(node, level, indent, prettierOpts) {
505518
prettierOpts
506519
)
507520
)
508-
formattedText = indentPrettierOutput(formattedText, level)
521+
formattedText = indentPrettierOutput(formattedText, level, indent)
509522
// in order to prevent parsing issues
510523
// https://github.com/inikulin/parse5/issues/262
511524
formattedText = formattedText.replace(/<\/script\s*>/g, '<\\/script>')
512525

513526
node.children = [
514-
{ type: 'text', value: single },
515-
{ type: 'text', value: formattedText },
516-
{ type: 'text', value: repeat(indent, level - 1) }
527+
{
528+
type: 'text',
529+
value: single
530+
},
531+
{
532+
type: 'text',
533+
value: formattedText
534+
},
535+
{
536+
type: 'text',
537+
value: repeat(indent, level - 1)
538+
}
517539
]
518540
}
519541
}
520542

521-
function indentPrettierOutput(formattedText, level) {
543+
function indentPrettierOutput(formattedText, level, indent) {
522544
let lines = formattedText.split(single)
523545

524546
for (let i = 0; i < lines.length; i++) {
525547
if (lines[i].replace(/\s+/g, '').length) {
526-
lines[i] = repeat(' ', level) + lines[i]
548+
lines[i] = repeat(indent, level) + lines[i]
527549
}
528550
}
529551

0 commit comments

Comments
 (0)