@takazudo/mdx-formatter 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hybrid-formatter.d.ts +4 -0
- package/dist/hybrid-formatter.js +34 -4
- package/dist/settings.js +2 -0
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -29,6 +29,10 @@ export declare class HybridFormatter {
|
|
|
29
29
|
collectJsxFormatOperations(operations: FormatterOperation[]): void;
|
|
30
30
|
needsJsxFormatting(node: MdxJsxElement, originalText: string): boolean;
|
|
31
31
|
formatJsxElement(node: MdxJsxElement, originalText: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Check if template literal indentation should be preserved based on settings.
|
|
34
|
+
*/
|
|
35
|
+
shouldPreserveTemplateLiteral(): boolean;
|
|
32
36
|
getAttributeString(attr: MdxJsxAttribute, originalText: string): string;
|
|
33
37
|
extractAttributeExpression(attrName: string, originalText: string): string | null;
|
|
34
38
|
extractExpressionValue(expr: MdxJsxAttributeValueExpression): string;
|
package/dist/hybrid-formatter.js
CHANGED
|
@@ -247,6 +247,11 @@ export class HybridFormatter {
|
|
|
247
247
|
return;
|
|
248
248
|
}
|
|
249
249
|
const endLine = jsxNode.position.end.line - 1;
|
|
250
|
+
// Skip JSX elements inside table rows
|
|
251
|
+
const currentLineContent = this.lines[endLine];
|
|
252
|
+
if (currentLineContent && currentLineContent.trim().startsWith('|')) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
250
255
|
if (endLine < this.lines.length - 1) {
|
|
251
256
|
const nextLine = this.lines[endLine + 1];
|
|
252
257
|
// Check if next line is text (not empty, not heading)
|
|
@@ -499,15 +504,21 @@ export class HybridFormatter {
|
|
|
499
504
|
// Add each attribute on its own line with proper indent
|
|
500
505
|
for (const attr of attributes) {
|
|
501
506
|
const attrStr = this.getAttributeString(attr, originalText);
|
|
502
|
-
// Handle multi-line expression values (like arrays)
|
|
507
|
+
// Handle multi-line expression values (like arrays, template literals)
|
|
503
508
|
if (attrStr.includes('\n')) {
|
|
504
509
|
const attrLines = attrStr.split('\n');
|
|
505
510
|
lines.push(`${indent}${attrLines[0]}`);
|
|
511
|
+
// Check if this is a template literal expression (backtick string)
|
|
512
|
+
// Template literal content has meaningful indentation that must be preserved
|
|
513
|
+
const isTemplateLiteral = this.shouldPreserveTemplateLiteral() && attrLines[0].includes('={`');
|
|
506
514
|
// Add subsequent lines with additional indentation for expression content
|
|
507
515
|
for (let i = 1; i < attrLines.length; i++) {
|
|
508
|
-
// Check if this line is part of the expression or the closing
|
|
509
516
|
const line = attrLines[i];
|
|
510
|
-
if (
|
|
517
|
+
if (isTemplateLiteral) {
|
|
518
|
+
// Preserve original indentation inside template literals
|
|
519
|
+
lines.push(line);
|
|
520
|
+
}
|
|
521
|
+
else if (line.trim().endsWith(']}') || line.trim() === ']}') {
|
|
511
522
|
// Closing of array expression
|
|
512
523
|
lines.push(`${indent}${line.trim()}`);
|
|
513
524
|
}
|
|
@@ -561,6 +572,12 @@ export class HybridFormatter {
|
|
|
561
572
|
}
|
|
562
573
|
return lines.join('\n');
|
|
563
574
|
}
|
|
575
|
+
/**
|
|
576
|
+
* Check if template literal indentation should be preserved based on settings.
|
|
577
|
+
*/
|
|
578
|
+
shouldPreserveTemplateLiteral() {
|
|
579
|
+
return this.settings.formatMultiLineJsx.preserveTemplateLiteralIndent !== false;
|
|
580
|
+
}
|
|
564
581
|
getAttributeString(attr, originalText) {
|
|
565
582
|
if (!attr || !attr.name)
|
|
566
583
|
return '';
|
|
@@ -573,7 +590,20 @@ export class HybridFormatter {
|
|
|
573
590
|
else if (attr.value && attr.value.type === 'mdxJsxAttributeValueExpression') {
|
|
574
591
|
// Expression value
|
|
575
592
|
const exprValue = this.extractExpressionValue(attr.value);
|
|
576
|
-
|
|
593
|
+
// For template literals, prefer extracting from original text to preserve
|
|
594
|
+
// internal indentation (AST normalizes/strips leading whitespace)
|
|
595
|
+
if (this.shouldPreserveTemplateLiteral() &&
|
|
596
|
+
exprValue &&
|
|
597
|
+
exprValue.trimStart().startsWith('`')) {
|
|
598
|
+
const extracted = this.extractAttributeExpression(attr.name, originalText);
|
|
599
|
+
if (extracted) {
|
|
600
|
+
result = extracted;
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
result += `={${exprValue}}`;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
else if (exprValue) {
|
|
577
607
|
result += `={${exprValue}}`;
|
|
578
608
|
}
|
|
579
609
|
else {
|
package/dist/settings.js
CHANGED
|
@@ -15,6 +15,8 @@ export const formatterSettings = {
|
|
|
15
15
|
indentSize: 2,
|
|
16
16
|
// Components to ignore (preserve their formatting completely)
|
|
17
17
|
ignoreComponents: [],
|
|
18
|
+
// Preserve indentation inside template literal JSX attributes (html={`...`}, css={`...`})
|
|
19
|
+
preserveTemplateLiteralIndent: true,
|
|
18
20
|
},
|
|
19
21
|
// Rule 3: Format all HTML blocks within MDX using Prettier
|
|
20
22
|
formatHtmlBlocksInMdx: {
|
package/dist/types.d.ts
CHANGED