@takazudo/mdx-formatter 0.3.0 → 0.4.0

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.
@@ -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;
@@ -499,15 +499,21 @@ export class HybridFormatter {
499
499
  // Add each attribute on its own line with proper indent
500
500
  for (const attr of attributes) {
501
501
  const attrStr = this.getAttributeString(attr, originalText);
502
- // Handle multi-line expression values (like arrays)
502
+ // Handle multi-line expression values (like arrays, template literals)
503
503
  if (attrStr.includes('\n')) {
504
504
  const attrLines = attrStr.split('\n');
505
505
  lines.push(`${indent}${attrLines[0]}`);
506
+ // Check if this is a template literal expression (backtick string)
507
+ // Template literal content has meaningful indentation that must be preserved
508
+ const isTemplateLiteral = this.shouldPreserveTemplateLiteral() && attrLines[0].includes('={`');
506
509
  // Add subsequent lines with additional indentation for expression content
507
510
  for (let i = 1; i < attrLines.length; i++) {
508
- // Check if this line is part of the expression or the closing
509
511
  const line = attrLines[i];
510
- if (line.trim().endsWith(']}') || line.trim() === ']}') {
512
+ if (isTemplateLiteral) {
513
+ // Preserve original indentation inside template literals
514
+ lines.push(line);
515
+ }
516
+ else if (line.trim().endsWith(']}') || line.trim() === ']}') {
511
517
  // Closing of array expression
512
518
  lines.push(`${indent}${line.trim()}`);
513
519
  }
@@ -561,6 +567,12 @@ export class HybridFormatter {
561
567
  }
562
568
  return lines.join('\n');
563
569
  }
570
+ /**
571
+ * Check if template literal indentation should be preserved based on settings.
572
+ */
573
+ shouldPreserveTemplateLiteral() {
574
+ return this.settings.formatMultiLineJsx.preserveTemplateLiteralIndent !== false;
575
+ }
564
576
  getAttributeString(attr, originalText) {
565
577
  if (!attr || !attr.name)
566
578
  return '';
@@ -573,7 +585,20 @@ export class HybridFormatter {
573
585
  else if (attr.value && attr.value.type === 'mdxJsxAttributeValueExpression') {
574
586
  // Expression value
575
587
  const exprValue = this.extractExpressionValue(attr.value);
576
- if (exprValue) {
588
+ // For template literals, prefer extracting from original text to preserve
589
+ // internal indentation (AST normalizes/strips leading whitespace)
590
+ if (this.shouldPreserveTemplateLiteral() &&
591
+ exprValue &&
592
+ exprValue.trimStart().startsWith('`')) {
593
+ const extracted = this.extractAttributeExpression(attr.name, originalText);
594
+ if (extracted) {
595
+ result = extracted;
596
+ }
597
+ else {
598
+ result += `={${exprValue}}`;
599
+ }
600
+ }
601
+ else if (exprValue) {
577
602
  result += `={${exprValue}}`;
578
603
  }
579
604
  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
@@ -76,6 +76,7 @@ export interface FormatMultiLineJsxSetting {
76
76
  indentSize: number;
77
77
  indentType?: string;
78
78
  ignoreComponents: string[];
79
+ preserveTemplateLiteralIndent: boolean;
79
80
  }
80
81
  export interface FormatHtmlBlocksInMdxSetting {
81
82
  enabled: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takazudo/mdx-formatter",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "AST-based markdown and MDX formatter with Japanese text support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",