@scrider/formatter 1.8.5 → 1.8.7
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/index.cjs +64 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +64 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -482,6 +482,8 @@ declare class BlockHandlerRegistry {
|
|
|
482
482
|
|
|
483
483
|
/** Block-level float on extended table host (4.2.10). */
|
|
484
484
|
type TableBlockFloat = 'left' | 'center' | 'right';
|
|
485
|
+
/** Block position when width is narrower than the line (margin-based, no float). */
|
|
486
|
+
type TableBlockAlign = 'left' | 'center' | 'right' | 'justify';
|
|
485
487
|
/**
|
|
486
488
|
* Column alignment options (GFM column-level subset).
|
|
487
489
|
*/
|
|
@@ -531,6 +533,8 @@ interface TableBlockData {
|
|
|
531
533
|
width?: number;
|
|
532
534
|
/** Block-level float on host wrapper (4.2.10). */
|
|
533
535
|
float?: TableBlockFloat;
|
|
536
|
+
/** Block position when width is set (margin-based, no float). */
|
|
537
|
+
blockAlign?: TableBlockAlign;
|
|
534
538
|
/** Column alignments */
|
|
535
539
|
colAligns?: (CellAlign | null)[];
|
|
536
540
|
/** Cells indexed by "row:col" */
|
package/dist/index.d.ts
CHANGED
|
@@ -482,6 +482,8 @@ declare class BlockHandlerRegistry {
|
|
|
482
482
|
|
|
483
483
|
/** Block-level float on extended table host (4.2.10). */
|
|
484
484
|
type TableBlockFloat = 'left' | 'center' | 'right';
|
|
485
|
+
/** Block position when width is narrower than the line (margin-based, no float). */
|
|
486
|
+
type TableBlockAlign = 'left' | 'center' | 'right' | 'justify';
|
|
485
487
|
/**
|
|
486
488
|
* Column alignment options (GFM column-level subset).
|
|
487
489
|
*/
|
|
@@ -531,6 +533,8 @@ interface TableBlockData {
|
|
|
531
533
|
width?: number;
|
|
532
534
|
/** Block-level float on host wrapper (4.2.10). */
|
|
533
535
|
float?: TableBlockFloat;
|
|
536
|
+
/** Block position when width is set (margin-based, no float). */
|
|
537
|
+
blockAlign?: TableBlockAlign;
|
|
534
538
|
/** Column alignments */
|
|
535
539
|
colAligns?: (CellAlign | null)[];
|
|
536
540
|
/** Cells indexed by "row:col" */
|
package/dist/index.js
CHANGED
|
@@ -627,6 +627,7 @@ function toCodeWidgetEmbedUrl(url) {
|
|
|
627
627
|
var EXT_TABLE_HOST_ATTR = "data-scrider-ext-table";
|
|
628
628
|
var EXT_TABLE_HOST_CLASS = "scrider-ext-table-host";
|
|
629
629
|
var VALID_TABLE_BLOCK_FLOATS = ["left", "center", "right"];
|
|
630
|
+
var VALID_TABLE_BLOCK_ALIGNS = ["left", "center", "right", "justify"];
|
|
630
631
|
var VALID_CELL_HORIZONTAL_ALIGNS = [
|
|
631
632
|
"left",
|
|
632
633
|
"center",
|
|
@@ -653,6 +654,18 @@ function getGridDimensions(cells) {
|
|
|
653
654
|
if (maxRow < 0 || maxCol < 0) return null;
|
|
654
655
|
return [maxRow + 1, maxCol + 1];
|
|
655
656
|
}
|
|
657
|
+
function rowspanCrossesHeaderBoundary(cells, headerRows) {
|
|
658
|
+
if (headerRows <= 0) return false;
|
|
659
|
+
for (const [key, cell] of Object.entries(cells)) {
|
|
660
|
+
if (cell === null) continue;
|
|
661
|
+
const parsed = parseCellKey(key);
|
|
662
|
+
if (!parsed) continue;
|
|
663
|
+
const [row] = parsed;
|
|
664
|
+
const rowspan = cell.rowspan ?? 1;
|
|
665
|
+
if (row < headerRows && row + rowspan > headerRows) return true;
|
|
666
|
+
}
|
|
667
|
+
return false;
|
|
668
|
+
}
|
|
656
669
|
function isValidCellData(cell) {
|
|
657
670
|
if (typeof cell !== "object" || cell === null || !Array.isArray(cell.ops) || cell.ops.length === 0) {
|
|
658
671
|
return false;
|
|
@@ -818,6 +831,12 @@ function parseTableBlockFloat(value) {
|
|
|
818
831
|
if (value === "left" || value === "center" || value === "right") return value;
|
|
819
832
|
return void 0;
|
|
820
833
|
}
|
|
834
|
+
function parseTableBlockAlign(value) {
|
|
835
|
+
if (value === "left" || value === "center" || value === "right" || value === "justify") {
|
|
836
|
+
return value;
|
|
837
|
+
}
|
|
838
|
+
return void 0;
|
|
839
|
+
}
|
|
821
840
|
function extractHostBlockWidthPx(host) {
|
|
822
841
|
const style = host.getAttribute("style") || "";
|
|
823
842
|
const widthMatch = style.match(/(?:^|;\s*)width:\s*([\d.]+)px/i);
|
|
@@ -828,25 +847,32 @@ function extractHostBlockWidthPx(host) {
|
|
|
828
847
|
}
|
|
829
848
|
function enrichTableBlockFromHost(data, host) {
|
|
830
849
|
const float = parseTableBlockFloat(host.getAttribute("data-float"));
|
|
850
|
+
const blockAlign = parseTableBlockAlign(host.getAttribute("data-block-align"));
|
|
831
851
|
const width = extractHostBlockWidthPx(host);
|
|
832
|
-
if (float == null && width == null) return data;
|
|
852
|
+
if (float == null && blockAlign == null && width == null) return data;
|
|
833
853
|
const next = { ...data };
|
|
834
854
|
if (float != null) next.float = float;
|
|
855
|
+
if (blockAlign != null && float == null) next.blockAlign = blockAlign;
|
|
835
856
|
if (width != null) next.width = width;
|
|
836
857
|
return next;
|
|
837
858
|
}
|
|
838
859
|
function tableNeedsHostWrapper(data) {
|
|
839
|
-
return data.float != null && VALID_TABLE_BLOCK_FLOATS.includes(data.float) || data.width != null && data.width > 0;
|
|
860
|
+
return data.float != null && VALID_TABLE_BLOCK_FLOATS.includes(data.float) || data.width != null && data.width > 0 || data.float == null && data.blockAlign != null && VALID_TABLE_BLOCK_ALIGNS.includes(data.blockAlign);
|
|
840
861
|
}
|
|
841
862
|
function renderTableHostWrapperOpen(data, pretty) {
|
|
842
863
|
const nl = pretty ? "\n" : "";
|
|
843
864
|
const ind = pretty ? " " : "";
|
|
844
865
|
const attrs = [`class="${EXT_TABLE_HOST_CLASS}"`, EXT_TABLE_HOST_ATTR];
|
|
845
866
|
if (data.float) attrs.push(`data-float="${escapeHtml(data.float)}"`);
|
|
867
|
+
if (data.blockAlign && !data.float) {
|
|
868
|
+
attrs.push(`data-block-align="${escapeHtml(data.blockAlign)}"`);
|
|
869
|
+
}
|
|
846
870
|
const styleParts = [];
|
|
847
871
|
if (data.width != null && data.width > 0) {
|
|
848
872
|
styleParts.push(`width: ${Math.round(data.width)}px`);
|
|
849
873
|
styleParts.push("max-width: 100%");
|
|
874
|
+
} else if (data.blockAlign === "justify" && !data.float) {
|
|
875
|
+
styleParts.push("width: 100%");
|
|
850
876
|
}
|
|
851
877
|
if (styleParts.length > 0) attrs.push(`style="${styleParts.join("; ")}"`);
|
|
852
878
|
return `<div ${attrs.join(" ")}>${nl}${ind}`;
|
|
@@ -867,6 +893,9 @@ function isGfmCompatible(data) {
|
|
|
867
893
|
if (data.float != null) {
|
|
868
894
|
return false;
|
|
869
895
|
}
|
|
896
|
+
if (data.blockAlign != null) {
|
|
897
|
+
return false;
|
|
898
|
+
}
|
|
870
899
|
for (const cell of Object.values(data.cells)) {
|
|
871
900
|
if (cell !== null && cell.align !== void 0) return false;
|
|
872
901
|
}
|
|
@@ -955,6 +984,26 @@ function collectTableRows(table) {
|
|
|
955
984
|
}
|
|
956
985
|
return { rows, headerRowCount };
|
|
957
986
|
}
|
|
987
|
+
function inferHeaderRowsFromThPrefix(rows) {
|
|
988
|
+
let count = 0;
|
|
989
|
+
for (const row of rows) {
|
|
990
|
+
let hasCell = false;
|
|
991
|
+
let allTh = true;
|
|
992
|
+
const cellChildren = row.childNodes;
|
|
993
|
+
for (let i = 0; i < cellChildren.length; i++) {
|
|
994
|
+
const cell = cellChildren[i];
|
|
995
|
+
if (!cell || !isElement(cell)) continue;
|
|
996
|
+
const tag = cell.tagName.toLowerCase();
|
|
997
|
+
if (tag !== "td" && tag !== "th") continue;
|
|
998
|
+
hasCell = true;
|
|
999
|
+
if (tag !== "th") allTh = false;
|
|
1000
|
+
}
|
|
1001
|
+
if (!hasCell) continue;
|
|
1002
|
+
if (!allTh) break;
|
|
1003
|
+
count++;
|
|
1004
|
+
}
|
|
1005
|
+
return count;
|
|
1006
|
+
}
|
|
958
1007
|
function extractColWidths(table) {
|
|
959
1008
|
const children = table.childNodes;
|
|
960
1009
|
for (let i = 0; i < children.length; i++) {
|
|
@@ -1042,7 +1091,13 @@ function extractInlineHorizontalAlign(element) {
|
|
|
1042
1091
|
return null;
|
|
1043
1092
|
}
|
|
1044
1093
|
function parseTableElement(table, context) {
|
|
1045
|
-
const
|
|
1094
|
+
const collected = collectTableRows(table);
|
|
1095
|
+
const { rows } = collected;
|
|
1096
|
+
let headerRowCount = collected.headerRowCount;
|
|
1097
|
+
if (headerRowCount === 0) {
|
|
1098
|
+
const inferred = inferHeaderRowsFromThPrefix(rows);
|
|
1099
|
+
if (inferred > 0) headerRowCount = inferred;
|
|
1100
|
+
}
|
|
1046
1101
|
if (rows.length === 0) return null;
|
|
1047
1102
|
const cells = {};
|
|
1048
1103
|
const colAligns = [];
|
|
@@ -1225,6 +1280,9 @@ var tableBlockHandler = {
|
|
|
1225
1280
|
if (data.float !== void 0 && !VALID_TABLE_BLOCK_FLOATS.includes(data.float)) {
|
|
1226
1281
|
return false;
|
|
1227
1282
|
}
|
|
1283
|
+
if (data.blockAlign !== void 0 && !VALID_TABLE_BLOCK_ALIGNS.includes(data.blockAlign)) {
|
|
1284
|
+
return false;
|
|
1285
|
+
}
|
|
1228
1286
|
if (data.colAligns !== void 0) {
|
|
1229
1287
|
if (!Array.isArray(data.colAligns) || data.colAligns.length !== cols) {
|
|
1230
1288
|
return false;
|
|
@@ -1260,14 +1318,15 @@ var tableBlockHandler = {
|
|
|
1260
1318
|
}
|
|
1261
1319
|
html += `${ind(1)}</colgroup>${nl}`;
|
|
1262
1320
|
}
|
|
1263
|
-
|
|
1321
|
+
const splitHeaderSection = headerRows > 0 && !rowspanCrossesHeaderBoundary(data.cells, headerRows);
|
|
1322
|
+
if (splitHeaderSection) {
|
|
1264
1323
|
html += `${ind(1)}<thead>${nl}`;
|
|
1265
1324
|
for (let r = 0; r < headerRows; r++) {
|
|
1266
1325
|
html += renderExtendedRow(data, r, cols, "th", context, pretty);
|
|
1267
1326
|
}
|
|
1268
1327
|
html += `${ind(1)}</thead>${nl}`;
|
|
1269
1328
|
}
|
|
1270
|
-
const bodyStart = headerRows;
|
|
1329
|
+
const bodyStart = splitHeaderSection ? headerRows : 0;
|
|
1271
1330
|
if (bodyStart < rows) {
|
|
1272
1331
|
html += `${ind(1)}<tbody>${nl}`;
|
|
1273
1332
|
for (let r = bodyStart; r < rows; r++) {
|