@tmlmt/cooklang-parser 3.0.0-alpha.13 → 3.0.0-alpha.14
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 +140 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +138 -4
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -32,9 +32,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
32
32
|
// src/index.ts
|
|
33
33
|
var index_exports = {};
|
|
34
34
|
__export(index_exports, {
|
|
35
|
+
BadIndentationError: () => BadIndentationError,
|
|
35
36
|
CategoryConfig: () => CategoryConfig,
|
|
36
37
|
NoProductCatalogForCartError: () => NoProductCatalogForCartError,
|
|
37
38
|
NoShoppingListForCartError: () => NoShoppingListForCartError,
|
|
39
|
+
NoTabAsIndentError: () => NoTabAsIndentError,
|
|
38
40
|
ProductCatalog: () => ProductCatalog,
|
|
39
41
|
Recipe: () => Recipe,
|
|
40
42
|
Section: () => Section,
|
|
@@ -354,6 +356,37 @@ var shoppingListRegex = d().literal("[").startNamedGroup("name").anyCharacter().
|
|
|
354
356
|
var rangeRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf(".,/").exactly(1).digit().oneOrMore().endGroup().optional().literal("-").digit().oneOrMore().startGroup().anyOf(".,/").exactly(1).digit().oneOrMore().endGroup().optional().endAnchor().toRegExp();
|
|
355
357
|
var numberLikeRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf(".,/").exactly(1).digit().oneOrMore().endGroup().optional().endAnchor().toRegExp();
|
|
356
358
|
var floatRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf(".").exactly(1).digit().oneOrMore().endGroup().optional().endAnchor().toRegExp();
|
|
359
|
+
var mdEscaped = d().literal("\\").startCaptureGroup().anyOf("*_`").endGroup();
|
|
360
|
+
var mdInlineCode = d().literal("`").startCaptureGroup().notAnyOf("`").oneOrMore().lazy().endGroup().literal("`");
|
|
361
|
+
var mdLink = d().literal("[").startCaptureGroup().notAnyOf("\\]").oneOrMore().lazy().endGroup().literal("](").startCaptureGroup().notAnyOf(")").oneOrMore().lazy().endGroup().literal(")");
|
|
362
|
+
var mdTripleAsterisk = d().literal("***").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("***");
|
|
363
|
+
var mdTripleUnderscore = d().literal("___").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("___");
|
|
364
|
+
var mdBoldAstItalicUnd = d().literal("**_").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("_**");
|
|
365
|
+
var mdBoldUndItalicAst = d().literal("__*").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("*__");
|
|
366
|
+
var mdItalicAstBoldUnd = d().literal("*__").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("__*");
|
|
367
|
+
var mdItalicUndBoldAst = d().literal("_**").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("**_");
|
|
368
|
+
var mdBoldAsterisk = d().literal("**").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("**");
|
|
369
|
+
var mdBoldUnderscore = d().wordBoundary().literal("__").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("__").wordBoundary();
|
|
370
|
+
var mdItalicAsterisk = d().literal("*").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("*");
|
|
371
|
+
var mdItalicUnderscore = d().wordBoundary().literal("_").startCaptureGroup().anyCharacter().oneOrMore().lazy().endGroup().literal("_").wordBoundary();
|
|
372
|
+
var markdownRegex = new RegExp(
|
|
373
|
+
[
|
|
374
|
+
mdEscaped,
|
|
375
|
+
mdInlineCode,
|
|
376
|
+
mdLink,
|
|
377
|
+
mdTripleAsterisk,
|
|
378
|
+
mdTripleUnderscore,
|
|
379
|
+
mdBoldAstItalicUnd,
|
|
380
|
+
mdBoldUndItalicAst,
|
|
381
|
+
mdItalicAstBoldUnd,
|
|
382
|
+
mdItalicUndBoldAst,
|
|
383
|
+
mdBoldAsterisk,
|
|
384
|
+
mdBoldUnderscore,
|
|
385
|
+
mdItalicAsterisk,
|
|
386
|
+
mdItalicUnderscore
|
|
387
|
+
].map((r2) => r2.toRegExp().source).join("|"),
|
|
388
|
+
"g"
|
|
389
|
+
);
|
|
357
390
|
|
|
358
391
|
// src/units/definitions.ts
|
|
359
392
|
var units = [
|
|
@@ -1480,12 +1513,106 @@ function parseQuantityInput(input_str) {
|
|
|
1480
1513
|
}
|
|
1481
1514
|
return { type: "fixed", value: parseFixedValue(clean_str) };
|
|
1482
1515
|
}
|
|
1516
|
+
function parseMarkdownSegments(text) {
|
|
1517
|
+
const items = [];
|
|
1518
|
+
let cursor = 0;
|
|
1519
|
+
for (const match of text.matchAll(markdownRegex)) {
|
|
1520
|
+
const idx = match.index;
|
|
1521
|
+
if (idx > cursor) {
|
|
1522
|
+
items.push({ type: "text", value: text.slice(cursor, idx) });
|
|
1523
|
+
}
|
|
1524
|
+
const [
|
|
1525
|
+
,
|
|
1526
|
+
escaped,
|
|
1527
|
+
// group 1: escaped character
|
|
1528
|
+
code,
|
|
1529
|
+
// group 2: inline code
|
|
1530
|
+
linkText,
|
|
1531
|
+
// group 3: link text
|
|
1532
|
+
linkUrl,
|
|
1533
|
+
// group 4: link url
|
|
1534
|
+
tripleAst,
|
|
1535
|
+
// group 5: ***bold+italic***
|
|
1536
|
+
tripleUnd,
|
|
1537
|
+
// group 6: ___bold+italic___
|
|
1538
|
+
astUnd,
|
|
1539
|
+
// group 7: **_bold+italic_**
|
|
1540
|
+
undAst,
|
|
1541
|
+
// group 8: __*bold+italic*__
|
|
1542
|
+
astUndUnd,
|
|
1543
|
+
// group 9: *__bold+italic__*
|
|
1544
|
+
undAstAst,
|
|
1545
|
+
// group 10: _**bold+italic**_
|
|
1546
|
+
boldAst,
|
|
1547
|
+
// group 11: **bold**
|
|
1548
|
+
boldUnd,
|
|
1549
|
+
// group 12: __bold__
|
|
1550
|
+
italicAst,
|
|
1551
|
+
// group 13: *italic*
|
|
1552
|
+
italicUnd
|
|
1553
|
+
// group 14: _italic_
|
|
1554
|
+
] = match;
|
|
1555
|
+
let value;
|
|
1556
|
+
let attribute;
|
|
1557
|
+
let href;
|
|
1558
|
+
if (escaped !== void 0) {
|
|
1559
|
+
items.push({ type: "text", value: escaped });
|
|
1560
|
+
cursor = idx + match[0].length;
|
|
1561
|
+
continue;
|
|
1562
|
+
} else if (code !== void 0) {
|
|
1563
|
+
value = code;
|
|
1564
|
+
attribute = "code";
|
|
1565
|
+
} else if (linkText !== void 0) {
|
|
1566
|
+
value = linkText;
|
|
1567
|
+
attribute = "link";
|
|
1568
|
+
href = linkUrl;
|
|
1569
|
+
} else if (tripleAst !== void 0 || tripleUnd !== void 0 || astUnd !== void 0 || undAst !== void 0 || astUndUnd !== void 0 || undAstAst !== void 0) {
|
|
1570
|
+
value = tripleAst ?? tripleUnd ?? astUnd ?? undAst ?? astUndUnd ?? undAstAst;
|
|
1571
|
+
attribute = "bold+italic";
|
|
1572
|
+
} else if (boldAst !== void 0 || boldUnd !== void 0) {
|
|
1573
|
+
value = boldAst ?? boldUnd;
|
|
1574
|
+
attribute = "bold";
|
|
1575
|
+
} else {
|
|
1576
|
+
value = italicAst ?? italicUnd;
|
|
1577
|
+
attribute = "italic";
|
|
1578
|
+
}
|
|
1579
|
+
const item = { type: "text", value };
|
|
1580
|
+
if (attribute) item.attribute = attribute;
|
|
1581
|
+
if (href) item.href = href;
|
|
1582
|
+
items.push(item);
|
|
1583
|
+
cursor = idx + match[0].length;
|
|
1584
|
+
}
|
|
1585
|
+
if (cursor < text.length) {
|
|
1586
|
+
items.push({ type: "text", value: text.slice(cursor) });
|
|
1587
|
+
}
|
|
1588
|
+
return items;
|
|
1589
|
+
}
|
|
1483
1590
|
function parseSimpleMetaVar(content, varName) {
|
|
1484
1591
|
const varMatch = content.match(
|
|
1485
1592
|
new RegExp(`^${varName}:\\s*(.*(?:\\r?\\n\\s+.*)*)+`, "m")
|
|
1486
1593
|
);
|
|
1487
1594
|
return varMatch ? varMatch[1]?.trim().replace(/\s*\r?\n\s+/g, " ") : void 0;
|
|
1488
1595
|
}
|
|
1596
|
+
function parseBlockScalarMetaVar(content, varName) {
|
|
1597
|
+
const match = content.match(
|
|
1598
|
+
new RegExp(
|
|
1599
|
+
`^${varName}:\\s*([|>])\\s*\\r?\\n((?:(?:[ ]+.*|\\s*)(?:\\r?\\n|$))+)`,
|
|
1600
|
+
"m"
|
|
1601
|
+
)
|
|
1602
|
+
);
|
|
1603
|
+
if (!match) return void 0;
|
|
1604
|
+
const style = match[1];
|
|
1605
|
+
const rawBlock = match[2];
|
|
1606
|
+
const lines = rawBlock.split(/\r?\n/);
|
|
1607
|
+
const firstNonEmpty = lines.find((l) => l.trim() !== "");
|
|
1608
|
+
if (!firstNonEmpty) return void 0;
|
|
1609
|
+
const baseIndent = firstNonEmpty.match(/^([ ]*)/)[1].length;
|
|
1610
|
+
const stripped = lines.map((line) => line.trim() === "" ? "" : line.slice(baseIndent)).join("\n").replace(/\n+$/, "");
|
|
1611
|
+
if (style === "|") {
|
|
1612
|
+
return stripped;
|
|
1613
|
+
}
|
|
1614
|
+
return stripped.replace(/\n\n/g, "\0").replace(/\n/g, " ").replace(/\0/g, "\n");
|
|
1615
|
+
}
|
|
1489
1616
|
function parseScalingMetaVar(content, varName) {
|
|
1490
1617
|
const varMatch = content.match(scalingMetaValueRegex(varName));
|
|
1491
1618
|
if (!varMatch) return void 0;
|
|
@@ -1668,6 +1795,13 @@ function extractMetadata(content) {
|
|
|
1668
1795
|
"cuisine",
|
|
1669
1796
|
"difficulty"
|
|
1670
1797
|
]) {
|
|
1798
|
+
if (metaVar === "description" || metaVar === "introduction") {
|
|
1799
|
+
const blockValue = parseBlockScalarMetaVar(metadataContent, metaVar);
|
|
1800
|
+
if (blockValue) {
|
|
1801
|
+
metadata[metaVar] = blockValue;
|
|
1802
|
+
continue;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1671
1805
|
const stringMetaValue = parseSimpleMetaVar(metadataContent, metaVar);
|
|
1672
1806
|
if (stringMetaValue) metadata[metaVar] = stringMetaValue;
|
|
1673
1807
|
}
|
|
@@ -2371,13 +2505,13 @@ var _Recipe = class _Recipe {
|
|
|
2371
2505
|
for (const match of text.matchAll(globalRegex)) {
|
|
2372
2506
|
const idx = match.index;
|
|
2373
2507
|
if (idx > cursor) {
|
|
2374
|
-
noteItems.push(
|
|
2508
|
+
noteItems.push(...parseMarkdownSegments(text.slice(cursor, idx)));
|
|
2375
2509
|
}
|
|
2376
2510
|
this._parseArbitraryScalable(match.groups, noteItems);
|
|
2377
2511
|
cursor = idx + match[0].length;
|
|
2378
2512
|
}
|
|
2379
2513
|
if (cursor < text.length) {
|
|
2380
|
-
noteItems.push(
|
|
2514
|
+
noteItems.push(...parseMarkdownSegments(text.slice(cursor)));
|
|
2381
2515
|
}
|
|
2382
2516
|
return noteItems;
|
|
2383
2517
|
}
|
|
@@ -2931,7 +3065,7 @@ var _Recipe = class _Recipe {
|
|
|
2931
3065
|
for (const match of line.matchAll(tokensRegex)) {
|
|
2932
3066
|
const idx = match.index;
|
|
2933
3067
|
if (idx > cursor) {
|
|
2934
|
-
items.push(
|
|
3068
|
+
items.push(...parseMarkdownSegments(line.slice(cursor, idx)));
|
|
2935
3069
|
}
|
|
2936
3070
|
const groups = match.groups;
|
|
2937
3071
|
if (groups.mIngredientName || groups.sIngredientName) {
|
|
@@ -2993,7 +3127,7 @@ var _Recipe = class _Recipe {
|
|
|
2993
3127
|
cursor = idx + match[0].length;
|
|
2994
3128
|
}
|
|
2995
3129
|
if (cursor < line.length) {
|
|
2996
|
-
items.push(
|
|
3130
|
+
items.push(...parseMarkdownSegments(line.slice(cursor)));
|
|
2997
3131
|
}
|
|
2998
3132
|
blankLineBefore = false;
|
|
2999
3133
|
}
|
|
@@ -3923,9 +4057,11 @@ function isAlternativeSelected(recipe, choices, item, alternativeIndex) {
|
|
|
3923
4057
|
}
|
|
3924
4058
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3925
4059
|
0 && (module.exports = {
|
|
4060
|
+
BadIndentationError,
|
|
3926
4061
|
CategoryConfig,
|
|
3927
4062
|
NoProductCatalogForCartError,
|
|
3928
4063
|
NoShoppingListForCartError,
|
|
4064
|
+
NoTabAsIndentError,
|
|
3929
4065
|
ProductCatalog,
|
|
3930
4066
|
Recipe,
|
|
3931
4067
|
Section,
|