@takazudo/mdx-formatter 0.4.3 → 0.5.0-next.2

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.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Browser-safe entry point for mdx-formatter.
3
+ *
4
+ * Unlike the main entry point (`index.ts`), this module does NOT import
5
+ * `load-config.ts` and therefore avoids pulling in Node's `fs` and `path`.
6
+ * Use this export (`@takazudo/mdx-formatter/browser`) when bundling for
7
+ * Vite, webpack, or any browser environment.
8
+ *
9
+ * By default, `formatHtmlBlocksInMdx` is **disabled** in browser mode because
10
+ * that rule depends on prettier, which is a Node.js dependency. If the code
11
+ * path is never reached, bundlers can tree-shake the prettier import away.
12
+ */
13
+ import { detectMdx } from './detect-mdx.js';
14
+ import type { FormatterSettings, DeepPartial } from './types.js';
15
+ export { detectMdx };
16
+ /**
17
+ * Format markdown/MDX content in browser environments.
18
+ *
19
+ * Uses browser-safe defaults (formatHtmlBlocksInMdx disabled).
20
+ * Pass an optional `settings` object to override individual rules.
21
+ */
22
+ export declare function format(content: string, settings?: DeepPartial<FormatterSettings>): Promise<string>;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Browser-safe entry point for mdx-formatter.
3
+ *
4
+ * Unlike the main entry point (`index.ts`), this module does NOT import
5
+ * `load-config.ts` and therefore avoids pulling in Node's `fs` and `path`.
6
+ * Use this export (`@takazudo/mdx-formatter/browser`) when bundling for
7
+ * Vite, webpack, or any browser environment.
8
+ *
9
+ * By default, `formatHtmlBlocksInMdx` is **disabled** in browser mode because
10
+ * that rule depends on prettier, which is a Node.js dependency. If the code
11
+ * path is never reached, bundlers can tree-shake the prettier import away.
12
+ */
13
+ import { HybridFormatter } from './hybrid-formatter.js';
14
+ import { detectMdx } from './detect-mdx.js';
15
+ import { formatterSettings } from './settings.js';
16
+ import { deepCloneSettings, deepMerge } from './utils.js';
17
+ export { detectMdx };
18
+ const MAX_ITERATIONS = 3;
19
+ // Browser-safe defaults: disable formatHtmlBlocksInMdx (requires prettier/Node.js)
20
+ const browserDefaults = {
21
+ ...formatterSettings,
22
+ formatHtmlBlocksInMdx: {
23
+ ...formatterSettings.formatHtmlBlocksInMdx,
24
+ enabled: false,
25
+ },
26
+ };
27
+ /**
28
+ * Format markdown/MDX content in browser environments.
29
+ *
30
+ * Uses browser-safe defaults (formatHtmlBlocksInMdx disabled).
31
+ * Pass an optional `settings` object to override individual rules.
32
+ */
33
+ export async function format(content, settings = {}) {
34
+ const merged = deepMerge(deepCloneSettings(browserDefaults), settings);
35
+ try {
36
+ let result = content;
37
+ for (let i = 0; i < MAX_ITERATIONS; i++) {
38
+ const formatter = new HybridFormatter(result, merged);
39
+ const formatted = await formatter.format();
40
+ if (formatted === result)
41
+ break;
42
+ result = formatted;
43
+ }
44
+ return result;
45
+ }
46
+ catch {
47
+ return content;
48
+ }
49
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Detect whether content is likely MDX.
3
+ *
4
+ * Shared between the main (`index.ts`) and browser (`browser.ts`) entry
5
+ * points so the detection logic is not duplicated.
6
+ */
7
+ export declare function detectMdx(content: string): boolean;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Detect whether content is likely MDX.
3
+ *
4
+ * Shared between the main (`index.ts`) and browser (`browser.ts`) entry
5
+ * points so the detection logic is not duplicated.
6
+ */
7
+ export function detectMdx(content) {
8
+ const mdxPatterns = [/^import\s+/m, /^export\s+/m, /<[A-Z]\w*[^>]*>/, /^\s*---\s*$/m];
9
+ return mdxPatterns.some((pattern) => pattern.test(content));
10
+ }
package/dist/index.d.ts CHANGED
@@ -2,11 +2,9 @@
2
2
  * Main entry point for the markdown formatter
3
3
  * Uses HybridFormatter for AST-based formatting
4
4
  */
5
+ import { detectMdx } from './detect-mdx.js';
5
6
  import type { FormatOptions } from './types.js';
6
- /**
7
- * Check if content is likely MDX
8
- */
9
- export declare function detectMdx(content: string): boolean;
7
+ export { detectMdx };
10
8
  /**
11
9
  * Format markdown/MDX content using hybrid AST approach
12
10
  */
package/dist/index.js CHANGED
@@ -5,14 +5,8 @@
5
5
  import { promises as fs } from 'fs';
6
6
  import { HybridFormatter } from './hybrid-formatter.js';
7
7
  import { loadConfig } from './load-config.js';
8
- /**
9
- * Check if content is likely MDX
10
- */
11
- export function detectMdx(content) {
12
- // Check for MDX-specific features
13
- const mdxPatterns = [/^import\s+/m, /^export\s+/m, /<[A-Z]\w*[^>]*>/, /^\s*---\s*$/m];
14
- return mdxPatterns.some((pattern) => pattern.test(content));
15
- }
8
+ import { detectMdx } from './detect-mdx.js';
9
+ export { detectMdx };
16
10
  /**
17
11
  * Format markdown/MDX content using hybrid AST approach
18
12
  */
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Wrapper for the Rust napi formatter.
3
+ * This module loads the native Rust formatter compiled via napi-rs
4
+ * and exposes the same format() API as the TypeScript implementation.
5
+ */
6
+ import type { FormatOptions } from './types.js';
7
+ /**
8
+ * Check if the Rust formatter is available
9
+ */
10
+ export declare function isRustFormatterAvailable(): boolean;
11
+ /**
12
+ * Format markdown/MDX content using the Rust formatter
13
+ * API-compatible with the TypeScript format() function
14
+ */
15
+ export declare function format(content: string, options?: FormatOptions): Promise<string>;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Wrapper for the Rust napi formatter.
3
+ * This module loads the native Rust formatter compiled via napi-rs
4
+ * and exposes the same format() API as the TypeScript implementation.
5
+ */
6
+ import { createRequire } from 'module';
7
+ import { loadConfig } from './load-config.js';
8
+ const require = createRequire(import.meta.url);
9
+ // Try to load the native module
10
+ let nativeFormat = null;
11
+ try {
12
+ const native = require('../crates/mdx-formatter-napi/mdx-formatter-napi.node');
13
+ nativeFormat = native.format;
14
+ }
15
+ catch {
16
+ // Native module not available - not built yet or wrong platform
17
+ }
18
+ /**
19
+ * Check if the Rust formatter is available
20
+ */
21
+ export function isRustFormatterAvailable() {
22
+ return nativeFormat !== null;
23
+ }
24
+ /**
25
+ * Format markdown/MDX content using the Rust formatter
26
+ * API-compatible with the TypeScript format() function
27
+ */
28
+ export async function format(content, options = {}) {
29
+ if (!nativeFormat) {
30
+ throw new Error('Rust formatter not available. Build it first with: cd crates/mdx-formatter-napi && cargo build');
31
+ }
32
+ const settings = loadConfig(options);
33
+ const settingsJson = JSON.stringify(settings);
34
+ return nativeFormat(content, settingsJson);
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takazudo/mdx-formatter",
3
- "version": "0.4.3",
3
+ "version": "0.5.0-next.2",
4
4
  "description": "AST-based markdown and MDX formatter with Japanese text support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -9,6 +9,10 @@
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
11
11
  "default": "./dist/index.js"
12
+ },
13
+ "./browser": {
14
+ "types": "./dist/browser.d.ts",
15
+ "default": "./dist/browser.js"
12
16
  }
13
17
  },
14
18
  "bin": {
@@ -33,29 +37,13 @@
33
37
  "lint:fix": "eslint --fix .",
34
38
  "check": "prettier --check . && eslint .",
35
39
  "check:fix": "prettier --write . && eslint --fix .",
40
+ "test:rust": "vitest run --config vitest.rust.config.ts",
41
+ "build:rust": "cd crates/mdx-formatter-napi && cargo build",
42
+ "build:rust:release": "cd crates/mdx-formatter-napi && cargo build --release",
36
43
  "b4push": "./scripts/run-b4push.sh",
37
44
  "doc:start": "pnpm --dir doc start",
38
- "prepare": "husky",
39
45
  "prepublishOnly": "tsc && vitest run"
40
46
  },
41
- "lint-staged": {
42
- "*.{js,mjs,ts}": [
43
- "prettier --write",
44
- "eslint --fix"
45
- ],
46
- "*.{json,yml,yaml}": [
47
- "prettier --write"
48
- ],
49
- ".claude/**/*.md": [
50
- "prettier --write"
51
- ],
52
- "doc/src/**/*.{ts,tsx,js,jsx,css}": [
53
- "prettier --write"
54
- ],
55
- "doc/docs/**/*.{md,mdx}": [
56
- "npx tsx src/cli.ts --write"
57
- ]
58
- },
59
47
  "keywords": [
60
48
  "markdown",
61
49
  "mdx",
@@ -102,8 +90,7 @@
102
90
  "@types/unist": "^3.0.3",
103
91
  "@vitest/coverage-v8": "^4.0.17",
104
92
  "eslint": "^9.39.2",
105
- "husky": "^9.1.7",
106
- "lint-staged": "^16.2.7",
93
+ "lefthook": "^2.1.4",
107
94
  "typescript": "^5.9.3",
108
95
  "typescript-eslint": "^8.54.0",
109
96
  "vfile": "^6.0.3",