@scrider/formatter 1.0.0 → 1.1.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.
- package/README.md +26 -5
- package/dist/index.cjs +56 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.js +54 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -98,16 +98,37 @@ normalizeDelta(delta) // Normalize operations
|
|
|
98
98
|
|
|
99
99
|
### Block Handlers
|
|
100
100
|
|
|
101
|
+
Block handlers process complex block embeds (tables, alerts, footnotes, etc.) stored in Delta as `{ insert: { block: { type, ... } } }`. Pass them to conversion functions via `createDefaultBlockHandlers()` or register individually:
|
|
102
|
+
|
|
101
103
|
```typescript
|
|
102
104
|
import {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
boxBlockHandler,
|
|
105
|
+
createDefaultBlockHandlers,
|
|
106
|
+
deltaToHtml,
|
|
107
|
+
htmlToDelta,
|
|
108
|
+
createDefaultRegistry,
|
|
108
109
|
} from '@scrider/formatter';
|
|
110
|
+
|
|
111
|
+
const registry = createDefaultRegistry();
|
|
112
|
+
const blockHandlers = createDefaultBlockHandlers();
|
|
113
|
+
|
|
114
|
+
// Delta with an alert block → HTML
|
|
115
|
+
const html = deltaToHtml(delta, { registry, blockHandlers });
|
|
116
|
+
// → '<div class="markdown-alert markdown-alert-note">...</div>'
|
|
117
|
+
|
|
118
|
+
// HTML with block embeds → Delta
|
|
119
|
+
const delta = htmlToDelta(html, { registry, blockHandlers });
|
|
109
120
|
```
|
|
110
121
|
|
|
122
|
+
Built-in handlers:
|
|
123
|
+
|
|
124
|
+
| Handler | Block type | Description |
|
|
125
|
+
|---------|-----------|-------------|
|
|
126
|
+
| `tableBlockHandler` | `table` | Extended tables with colspan/rowspan, nested Delta cells |
|
|
127
|
+
| `alertBlockHandler` | `alert` | GitHub-style alerts (`[!NOTE]`, `[!TIP]`, `[!WARNING]`, etc.) |
|
|
128
|
+
| `footnotesBlockHandler` | `footnotes` | Footnotes with `[^id]` references |
|
|
129
|
+
| `columnsBlockHandler` | `columns` | Multi-column layout (CSS Grid) |
|
|
130
|
+
| `boxBlockHandler` | `box` | Inline-box with float/overflow |
|
|
131
|
+
|
|
111
132
|
## Ecosystem
|
|
112
133
|
|
|
113
134
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -63,6 +63,7 @@ __export(index_exports, {
|
|
|
63
63
|
dividerFormat: () => dividerFormat,
|
|
64
64
|
escapeHtml: () => escapeHtml,
|
|
65
65
|
extractBoxOpAttributes: () => extractBoxOpAttributes,
|
|
66
|
+
fontFormat: () => fontFormat,
|
|
66
67
|
footnoteRefFormat: () => footnoteRefFormat,
|
|
67
68
|
footnotesBlockHandler: () => footnotesBlockHandler,
|
|
68
69
|
formulaFormat: () => formulaFormat,
|
|
@@ -89,6 +90,7 @@ __export(index_exports, {
|
|
|
89
90
|
nodeAdapter: () => nodeAdapter,
|
|
90
91
|
normalizeDelta: () => normalizeDelta,
|
|
91
92
|
sanitizeDelta: () => sanitizeDelta,
|
|
93
|
+
sizeFormat: () => sizeFormat,
|
|
92
94
|
slugify: () => slugify,
|
|
93
95
|
slugifyWithDedup: () => slugifyWithDedup,
|
|
94
96
|
strikeFormat: () => strikeFormat,
|
|
@@ -1663,6 +1665,15 @@ var colorFormat = {
|
|
|
1663
1665
|
}
|
|
1664
1666
|
};
|
|
1665
1667
|
|
|
1668
|
+
// src/schema/formats/inline/font.ts
|
|
1669
|
+
var fontFormat = {
|
|
1670
|
+
name: "font",
|
|
1671
|
+
scope: "inline",
|
|
1672
|
+
validate(value) {
|
|
1673
|
+
return typeof value === "string" && value.length > 0;
|
|
1674
|
+
}
|
|
1675
|
+
};
|
|
1676
|
+
|
|
1666
1677
|
// src/schema/formats/inline/italic.ts
|
|
1667
1678
|
var italicFormat = {
|
|
1668
1679
|
name: "italic",
|
|
@@ -1720,6 +1731,15 @@ var markFormat = {
|
|
|
1720
1731
|
}
|
|
1721
1732
|
};
|
|
1722
1733
|
|
|
1734
|
+
// src/schema/formats/inline/size.ts
|
|
1735
|
+
var sizeFormat = {
|
|
1736
|
+
name: "size",
|
|
1737
|
+
scope: "inline",
|
|
1738
|
+
validate(value) {
|
|
1739
|
+
return typeof value === "string" && value.length > 0;
|
|
1740
|
+
}
|
|
1741
|
+
};
|
|
1742
|
+
|
|
1723
1743
|
// src/schema/formats/inline/strike.ts
|
|
1724
1744
|
var strikeFormat = {
|
|
1725
1745
|
name: "strike",
|
|
@@ -1927,7 +1947,9 @@ var INLINE_FORMAT_ORDER = [
|
|
|
1927
1947
|
];
|
|
1928
1948
|
var INLINE_STYLE_FORMATS = {
|
|
1929
1949
|
color: "color",
|
|
1930
|
-
background: "background-color"
|
|
1950
|
+
background: "background-color",
|
|
1951
|
+
font: "font-family",
|
|
1952
|
+
size: "font-size"
|
|
1931
1953
|
};
|
|
1932
1954
|
var BLOCK_FORMAT_TAGS = {
|
|
1933
1955
|
header: (value) => `h${String(value)}`,
|
|
@@ -2312,6 +2334,8 @@ var defaultInlineFormats = [
|
|
|
2312
2334
|
linkFormat,
|
|
2313
2335
|
colorFormat,
|
|
2314
2336
|
backgroundFormat,
|
|
2337
|
+
fontFormat,
|
|
2338
|
+
sizeFormat,
|
|
2315
2339
|
markFormat,
|
|
2316
2340
|
kbdFormat
|
|
2317
2341
|
];
|
|
@@ -3342,6 +3366,14 @@ function htmlToDelta(html, options = {}) {
|
|
|
3342
3366
|
if (bg) {
|
|
3343
3367
|
currentAttributes.background = bg;
|
|
3344
3368
|
}
|
|
3369
|
+
const fontFamily = element.style?.fontFamily || element.style?.getPropertyValue?.("font-family");
|
|
3370
|
+
if (fontFamily) {
|
|
3371
|
+
currentAttributes.font = fontFamily.replace(/^["']|["']$/g, "");
|
|
3372
|
+
}
|
|
3373
|
+
const fontSize = element.style?.fontSize || element.style?.getPropertyValue?.("font-size");
|
|
3374
|
+
if (fontSize) {
|
|
3375
|
+
currentAttributes.size = fontSize;
|
|
3376
|
+
}
|
|
3345
3377
|
processChildren(element);
|
|
3346
3378
|
flushText();
|
|
3347
3379
|
currentAttributes = prevAttrs;
|
|
@@ -4043,11 +4075,14 @@ function renderInlineText2(text, attributes) {
|
|
|
4043
4075
|
if (link) {
|
|
4044
4076
|
result = renderLink(result, link);
|
|
4045
4077
|
}
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4078
|
+
const styleProps = [];
|
|
4079
|
+
if (typeof attributes.color === "string") styleProps.push(`color: ${attributes.color}`);
|
|
4080
|
+
if (typeof attributes.background === "string")
|
|
4081
|
+
styleProps.push(`background-color: ${attributes.background}`);
|
|
4082
|
+
if (typeof attributes.font === "string") styleProps.push(`font-family: ${attributes.font}`);
|
|
4083
|
+
if (typeof attributes.size === "string") styleProps.push(`font-size: ${attributes.size}`);
|
|
4084
|
+
if (styleProps.length > 0) {
|
|
4085
|
+
result = `<span style="${styleProps.join("; ")}">${result}</span>`;
|
|
4051
4086
|
}
|
|
4052
4087
|
return result;
|
|
4053
4088
|
}
|
|
@@ -4789,6 +4824,19 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
|
|
|
4789
4824
|
currentInlineAttrs = { ...currentInlineAttrs, background: bgMatch[1].trim() };
|
|
4790
4825
|
addedKeys.push("background");
|
|
4791
4826
|
}
|
|
4827
|
+
const fontMatch = style.match(/(?:^|;)\s*font-family\s*:\s*([^;]+)/i);
|
|
4828
|
+
if (fontMatch) {
|
|
4829
|
+
currentInlineAttrs = {
|
|
4830
|
+
...currentInlineAttrs,
|
|
4831
|
+
font: fontMatch[1].trim().replace(/^["']|["']$/g, "")
|
|
4832
|
+
};
|
|
4833
|
+
addedKeys.push("font");
|
|
4834
|
+
}
|
|
4835
|
+
const sizeMatch = style.match(/(?:^|;)\s*font-size\s*:\s*([^;]+)/i);
|
|
4836
|
+
if (sizeMatch) {
|
|
4837
|
+
currentInlineAttrs = { ...currentInlineAttrs, size: sizeMatch[1].trim() };
|
|
4838
|
+
addedKeys.push("size");
|
|
4839
|
+
}
|
|
4792
4840
|
spanAttrStack.push(addedKeys);
|
|
4793
4841
|
return;
|
|
4794
4842
|
}
|
|
@@ -4900,6 +4948,7 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
|
|
|
4900
4948
|
dividerFormat,
|
|
4901
4949
|
escapeHtml,
|
|
4902
4950
|
extractBoxOpAttributes,
|
|
4951
|
+
fontFormat,
|
|
4903
4952
|
footnoteRefFormat,
|
|
4904
4953
|
footnotesBlockHandler,
|
|
4905
4954
|
formulaFormat,
|
|
@@ -4926,6 +4975,7 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
|
|
|
4926
4975
|
nodeAdapter,
|
|
4927
4976
|
normalizeDelta,
|
|
4928
4977
|
sanitizeDelta,
|
|
4978
|
+
sizeFormat,
|
|
4929
4979
|
slugify,
|
|
4930
4980
|
slugifyWithDedup,
|
|
4931
4981
|
strikeFormat,
|