@lvce-editor/markdown-worker 2.1.0 → 3.0.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.
@@ -1122,68 +1122,68 @@ const Tfoot = 'tfoot';
1122
1122
 
1123
1123
  const getVirtualDomTag = text => {
1124
1124
  switch (text) {
1125
- case H1:
1126
- return H1$1;
1127
- case H2:
1128
- return H2$1;
1129
- case H3:
1130
- return H3$1;
1131
- case H4:
1132
- return H4$1;
1133
- case H5:
1134
- return H5$1;
1135
- case Div:
1136
- return Div$1;
1125
+ case A:
1126
+ return A$1;
1127
+ case Abbr:
1128
+ return Abbr$1;
1137
1129
  case Article:
1138
1130
  return Article$1;
1139
1131
  case Aside:
1140
1132
  return Aside$1;
1141
- case Footer:
1142
- return Footer$1;
1143
- case Header:
1144
- return Header$1;
1145
- case Nav:
1146
- return Nav$1;
1147
- case Section:
1148
- return Section$1;
1149
- case Search:
1150
- return Search$1;
1133
+ case Br:
1134
+ return Br$1;
1135
+ case Cite:
1136
+ return Cite$1;
1137
+ case Data:
1138
+ return Data$1;
1151
1139
  case Dd:
1152
1140
  return Dd$1;
1141
+ case Div:
1142
+ return Div$1;
1153
1143
  case Dl:
1154
1144
  return Dl$1;
1155
1145
  case Figcaption:
1156
1146
  return Figcaption$1;
1157
1147
  case Figure:
1158
1148
  return Figure$1;
1149
+ case Footer:
1150
+ return Footer$1;
1151
+ case H1:
1152
+ return H1$1;
1153
+ case H2:
1154
+ return H2$1;
1155
+ case H3:
1156
+ return H3$1;
1157
+ case H4:
1158
+ return H4$1;
1159
+ case H5:
1160
+ return H5$1;
1161
+ case Header:
1162
+ return Header$1;
1159
1163
  case Hr:
1160
1164
  return Hr$1;
1165
+ case Img:
1166
+ return Img$1;
1161
1167
  case Li:
1162
1168
  return Li$1;
1169
+ case Nav:
1170
+ return Nav$1;
1163
1171
  case Ol:
1164
1172
  return Ol$1;
1165
1173
  case P$1:
1166
1174
  return P$2;
1167
1175
  case Pre:
1168
1176
  return Pre$1;
1169
- case A:
1170
- return A$1;
1171
- case Abbr:
1172
- return Abbr$1;
1173
- case Br:
1174
- return Br$1;
1175
- case Cite:
1176
- return Cite$1;
1177
- case Data:
1178
- return Data$1;
1179
- case Time:
1180
- return Time$1;
1181
- case Tfoot:
1182
- return Tfoot$1;
1183
- case Img:
1184
- return Img$1;
1177
+ case Search:
1178
+ return Search$1;
1179
+ case Section:
1180
+ return Section$1;
1185
1181
  case Span:
1186
1182
  return Span$1;
1183
+ case Tfoot:
1184
+ return Tfoot$1;
1185
+ case Time:
1186
+ return Time$1;
1187
1187
  default:
1188
1188
  return Div$1;
1189
1189
  }
@@ -1233,19 +1233,19 @@ class UnexpectedTokenError extends Error {
1233
1233
  }
1234
1234
 
1235
1235
  const State = {
1236
- TopLevelContent: 1,
1237
- AfterOpeningAngleBracket: 2,
1238
- InsideOpeningTag: 3,
1239
- AfterClosingTagSlash: 4,
1240
- AfterClosingTagName: 5,
1241
- InsideOpeningTagAfterWhitespace: 6,
1242
- AfterAttributeName: 7,
1243
1236
  AfterAttributeEqualSign: 8,
1244
- InsideAttributeAfterDoubleQuote: 9,
1245
- AfterAttributeValueInsideDoubleQuote: 10,
1237
+ AfterAttributeName: 7,
1246
1238
  AfterAttributeValueClosingQuote: 11,
1239
+ AfterAttributeValueInsideDoubleQuote: 10,
1240
+ AfterClosingTagName: 5,
1241
+ AfterClosingTagSlash: 4,
1247
1242
  AfterExclamationMark: 16,
1248
- InsideComment: 17
1243
+ AfterOpeningAngleBracket: 2,
1244
+ InsideAttributeAfterDoubleQuote: 9,
1245
+ InsideComment: 17,
1246
+ InsideOpeningTag: 3,
1247
+ InsideOpeningTagAfterWhitespace: 6,
1248
+ TopLevelContent: 1
1249
1249
  };
1250
1250
  const RE_ANGLE_BRACKET_OPEN = /^</;
1251
1251
  const RE_ANGLE_BRACKET_OPEN_TAG = /^<(?![\s!%])/;
@@ -1266,6 +1266,7 @@ const RE_BLOCK_COMMENT_CONTENT = /^[a-zA-Z\s]+/;
1266
1266
  const RE_COMMENT_END = /^-->/;
1267
1267
  const RE_TAG_TEXT = /^[^\s>]+/;
1268
1268
  const RE_ANY_TEXT = /^[^\n]+/;
1269
+ const RE_ATTRIBUTE_TEXT = /^[^\n<>/\s]+/;
1269
1270
  const RE_BLOCK_COMMENT_START = /^<!--/;
1270
1271
  const RE_SELF_CLOSING = /^\/>/;
1271
1272
  const tokenizeHtml = text => {
@@ -1278,26 +1279,97 @@ const tokenizeHtml = text => {
1278
1279
  while (index < text.length) {
1279
1280
  const part = text.slice(index);
1280
1281
  switch (state) {
1281
- case State.TopLevelContent:
1282
- if (next = part.match(RE_ANGLE_BRACKET_OPEN_TAG)) {
1282
+ case State.AfterAttributeEqualSign:
1283
+ if (next = part.match(RE_DOUBLE_QUOTE)) {
1284
+ token = AttributeQuoteStart;
1285
+ state = State.InsideAttributeAfterDoubleQuote;
1286
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1287
+ token = ClosingAngleBracket;
1288
+ state = State.TopLevelContent;
1289
+ } else if (next = part.match(RE_ATTRIBUTE_TEXT)) {
1290
+ token = AttributeValue;
1291
+ state = State.InsideOpeningTag;
1292
+ } else {
1293
+ throw new UnexpectedTokenError();
1294
+ }
1295
+ break;
1296
+ case State.AfterAttributeName:
1297
+ if (next = part.match(RE_EQUAL_SIGN)) {
1298
+ token = AttributeEqualSign;
1299
+ state = State.AfterAttributeEqualSign;
1300
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1301
+ token = ClosingAngleBracket;
1302
+ state = State.TopLevelContent;
1303
+ } else if (next = part.match(RE_WHITESPACE)) {
1304
+ token = WhitespaceInsideOpeningTag;
1305
+ state = State.InsideOpeningTagAfterWhitespace;
1306
+ } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1283
1307
  token = OpeningAngleBracket;
1284
1308
  state = State.AfterOpeningAngleBracket;
1285
- } else if (next = part.match(RE_CONTENT)) {
1286
- token = Content;
1309
+ } else {
1310
+ text.slice(index); // ?
1311
+ throw new UnexpectedTokenError();
1312
+ }
1313
+ break;
1314
+ case State.AfterAttributeValueClosingQuote:
1315
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1316
+ token = ClosingAngleBracket;
1287
1317
  state = State.TopLevelContent;
1288
- } else if (next = part.match(RE_BLOCK_COMMENT_START)) {
1289
- token = CommentStart;
1290
- state = State.InsideComment;
1291
- } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1318
+ } else if (next = part.match(RE_WHITESPACE)) {
1319
+ token = WhitespaceInsideOpeningTag;
1320
+ state = State.InsideOpeningTagAfterWhitespace;
1321
+ } else if (next = part.match(RE_SELF_CLOSING)) {
1322
+ token = ClosingAngleBracket;
1323
+ state = State.TopLevelContent;
1324
+ } else {
1325
+ throw new UnexpectedTokenError();
1326
+ }
1327
+ break;
1328
+ case State.AfterAttributeValueInsideDoubleQuote:
1329
+ if (next = part.match(RE_DOUBLE_QUOTE)) {
1330
+ token = AttributeQuoteEnd;
1331
+ state = State.AfterAttributeValueClosingQuote;
1332
+ } else {
1333
+ throw new UnexpectedTokenError();
1334
+ }
1335
+ break;
1336
+ case State.AfterClosingTagName:
1337
+ if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1338
+ token = ClosingAngleBracket;
1339
+ state = State.TopLevelContent;
1340
+ } else if (next = part.match(RE_WHITESPACE)) {
1292
1341
  token = Content;
1293
1342
  state = State.TopLevelContent;
1294
- } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1295
- token = Text;
1343
+ } else {
1344
+ throw new UnexpectedTokenError();
1345
+ }
1346
+ break;
1347
+ case State.AfterClosingTagSlash:
1348
+ if (next = part.match(RE_TAGNAME)) {
1349
+ token = TagNameEnd;
1350
+ state = State.AfterClosingTagName;
1351
+ } else if (next = part.match(RE_WHITESPACE)) {
1352
+ token = WhitespaceAfterClosingTagSlash;
1353
+ state = State.TopLevelContent;
1354
+ } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1355
+ token = ClosingAngleBracket;
1296
1356
  state = State.TopLevelContent;
1297
1357
  } else {
1298
1358
  throw new UnexpectedTokenError();
1299
1359
  }
1300
1360
  break;
1361
+ case State.AfterExclamationMark:
1362
+ if (next = part.match(RE_DASH_DASH)) {
1363
+ token = StartCommentDashes;
1364
+ state = State.InsideComment;
1365
+ } else if (next = part.match(RE_DOCTYPE)) {
1366
+ token = Doctype;
1367
+ state = State.InsideOpeningTag;
1368
+ } else {
1369
+ text.slice(index); // ?
1370
+ throw new UnexpectedTokenError();
1371
+ }
1372
+ break;
1301
1373
  case State.AfterOpeningAngleBracket:
1302
1374
  if (next = part.match(RE_TAGNAME)) {
1303
1375
  token = TagNameStart;
@@ -1322,15 +1394,14 @@ const tokenizeHtml = text => {
1322
1394
  throw new UnexpectedTokenError();
1323
1395
  }
1324
1396
  break;
1325
- case State.AfterExclamationMark:
1326
- if (next = part.match(RE_DASH_DASH)) {
1327
- token = StartCommentDashes;
1328
- state = State.InsideComment;
1329
- } else if (next = part.match(RE_DOCTYPE)) {
1330
- token = Doctype;
1331
- state = State.InsideOpeningTag;
1397
+ case State.InsideAttributeAfterDoubleQuote:
1398
+ if (next = text.slice(index).match(RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE)) {
1399
+ token = AttributeValue;
1400
+ state = State.AfterAttributeValueInsideDoubleQuote;
1401
+ } else if (next = part.match(RE_DOUBLE_QUOTE)) {
1402
+ token = AttributeQuoteEnd;
1403
+ state = State.AfterAttributeValueClosingQuote;
1332
1404
  } else {
1333
- text.slice(index); // ?
1334
1405
  throw new UnexpectedTokenError();
1335
1406
  }
1336
1407
  break;
@@ -1379,89 +1450,22 @@ const tokenizeHtml = text => {
1379
1450
  throw new UnexpectedTokenError();
1380
1451
  }
1381
1452
  break;
1382
- case State.AfterAttributeName:
1383
- if (next = part.match(RE_EQUAL_SIGN)) {
1384
- token = AttributeEqualSign;
1385
- state = State.AfterAttributeEqualSign;
1386
- } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1387
- token = ClosingAngleBracket;
1388
- state = State.TopLevelContent;
1389
- } else if (next = part.match(RE_WHITESPACE)) {
1390
- token = WhitespaceInsideOpeningTag;
1391
- state = State.InsideOpeningTagAfterWhitespace;
1392
- } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1453
+ case State.TopLevelContent:
1454
+ if (next = part.match(RE_ANGLE_BRACKET_OPEN_TAG)) {
1393
1455
  token = OpeningAngleBracket;
1394
1456
  state = State.AfterOpeningAngleBracket;
1395
- } else {
1396
- text.slice(index); // ?
1397
- throw new UnexpectedTokenError();
1398
- }
1399
- break;
1400
- case State.AfterAttributeEqualSign:
1401
- if (next = part.match(RE_DOUBLE_QUOTE)) {
1402
- token = AttributeQuoteStart;
1403
- state = State.InsideAttributeAfterDoubleQuote;
1404
- } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1405
- token = ClosingAngleBracket;
1406
- state = State.TopLevelContent;
1407
- } else {
1408
- throw new UnexpectedTokenError();
1409
- }
1410
- break;
1411
- case State.InsideAttributeAfterDoubleQuote:
1412
- if (next = text.slice(index).match(RE_ATTRIBUTE_VALUE_INSIDE_DOUBLE_QUOTE)) {
1413
- token = AttributeValue;
1414
- state = State.AfterAttributeValueInsideDoubleQuote;
1415
- } else if (next = part.match(RE_DOUBLE_QUOTE)) {
1416
- token = AttributeQuoteEnd;
1417
- state = State.AfterAttributeValueClosingQuote;
1418
- } else {
1419
- throw new UnexpectedTokenError();
1420
- }
1421
- break;
1422
- case State.AfterAttributeValueInsideDoubleQuote:
1423
- if (next = part.match(RE_DOUBLE_QUOTE)) {
1424
- token = AttributeQuoteEnd;
1425
- state = State.AfterAttributeValueClosingQuote;
1426
- } else {
1427
- throw new UnexpectedTokenError();
1428
- }
1429
- break;
1430
- case State.AfterAttributeValueClosingQuote:
1431
- if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1432
- token = ClosingAngleBracket;
1433
- state = State.TopLevelContent;
1434
- } else if (next = part.match(RE_WHITESPACE)) {
1435
- token = WhitespaceInsideOpeningTag;
1436
- state = State.InsideOpeningTagAfterWhitespace;
1437
- } else if (next = part.match(RE_SELF_CLOSING)) {
1438
- token = ClosingAngleBracket;
1439
- state = State.TopLevelContent;
1440
- } else {
1441
- throw new UnexpectedTokenError();
1442
- }
1443
- break;
1444
- case State.AfterClosingTagSlash:
1445
- if (next = part.match(RE_TAGNAME)) {
1446
- token = TagNameEnd;
1447
- state = State.AfterClosingTagName;
1448
- } else if (next = part.match(RE_WHITESPACE)) {
1449
- token = WhitespaceAfterClosingTagSlash;
1457
+ } else if (next = part.match(RE_CONTENT)) {
1458
+ token = Content;
1450
1459
  state = State.TopLevelContent;
1460
+ } else if (next = part.match(RE_BLOCK_COMMENT_START)) {
1461
+ token = CommentStart;
1462
+ state = State.InsideComment;
1451
1463
  } else if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1452
- token = ClosingAngleBracket;
1453
- state = State.TopLevelContent;
1454
- } else {
1455
- throw new UnexpectedTokenError();
1456
- }
1457
- break;
1458
- case State.AfterClosingTagName:
1459
- if (next = part.match(RE_ANGLE_BRACKET_CLOSE)) {
1460
- token = ClosingAngleBracket;
1461
- state = State.TopLevelContent;
1462
- } else if (next = part.match(RE_WHITESPACE)) {
1463
1464
  token = Content;
1464
1465
  state = State.TopLevelContent;
1466
+ } else if (next = part.match(RE_ANGLE_BRACKET_OPEN)) {
1467
+ token = Text;
1468
+ state = State.TopLevelContent;
1465
1469
  } else {
1466
1470
  throw new UnexpectedTokenError();
1467
1471
  }
@@ -1471,8 +1475,8 @@ const tokenizeHtml = text => {
1471
1475
  }
1472
1476
  const tokenText = next[0];
1473
1477
  tokens.push({
1474
- type: token,
1475
- text: tokenText
1478
+ text: tokenText,
1479
+ type: token
1476
1480
  });
1477
1481
  index += tokenText.length;
1478
1482
  }
@@ -1485,33 +1489,14 @@ const parseHtml = (html, allowedAttributes) => {
1485
1489
  const tokens = tokenizeHtml(html);
1486
1490
  const dom = [];
1487
1491
  const root = {
1488
- type: 0,
1489
- childCount: 0
1492
+ childCount: 0,
1493
+ type: 0
1490
1494
  };
1491
1495
  let current = root;
1492
1496
  const stack = [root];
1493
1497
  let attributeName = '';
1494
1498
  for (const token of tokens) {
1495
1499
  switch (token.type) {
1496
- case TagNameStart:
1497
- current.childCount++;
1498
- current = {
1499
- type: getVirtualDomTag(token.text),
1500
- childCount: 0
1501
- };
1502
- dom.push(current);
1503
- if (!isSelfClosingTag(token.text)) {
1504
- stack.push(current);
1505
- }
1506
- break;
1507
- case TagNameEnd:
1508
- stack.pop();
1509
- current = stack.at(-1) || root;
1510
- break;
1511
- case Content:
1512
- current.childCount++;
1513
- dom.push(text(parseText(token.text)));
1514
- break;
1515
1500
  case AttributeName:
1516
1501
  attributeName = token.text;
1517
1502
  if (attributeName === 'class') {
@@ -1524,6 +1509,25 @@ const parseHtml = (html, allowedAttributes) => {
1524
1509
  }
1525
1510
  attributeName = '';
1526
1511
  break;
1512
+ case Content:
1513
+ current.childCount++;
1514
+ dom.push(text(parseText(token.text)));
1515
+ break;
1516
+ case TagNameEnd:
1517
+ stack.pop();
1518
+ current = stack.at(-1) || root;
1519
+ break;
1520
+ case TagNameStart:
1521
+ current.childCount++;
1522
+ current = {
1523
+ childCount: 0,
1524
+ type: getVirtualDomTag(token.text)
1525
+ };
1526
+ dom.push(current);
1527
+ if (!isSelfClosingTag(token.text)) {
1528
+ stack.push(current);
1529
+ }
1530
+ break;
1527
1531
  }
1528
1532
  }
1529
1533
  return dom;
@@ -1534,11 +1538,11 @@ const getMarkdownVirtualDom = html => {
1534
1538
  const childDom = parseHtml(html, allowedMarkdownAttributes);
1535
1539
  const markdownChildCount = getVirtualDomChildCount(childDom);
1536
1540
  return [{
1537
- type: Div$1,
1541
+ childCount: markdownChildCount,
1538
1542
  className: Markdown,
1539
- role: Document,
1540
1543
  onContextMenu: HandleReadmeContextMenu,
1541
- childCount: markdownChildCount
1544
+ role: Document,
1545
+ type: Div$1
1542
1546
  }, ...childDom];
1543
1547
  };
1544
1548
 
@@ -3227,6 +3231,17 @@ const renderMarkdown$1 = async (markdown, options = {}) => {
3227
3231
  return localLink ? html : html.replace(RE_LINK_START, `<a target="_blank" rel="noreferrer noopener nofollow" `);
3228
3232
  };
3229
3233
  }
3234
+ const {
3235
+ baseUrl
3236
+ } = options;
3237
+ if (baseUrl) {
3238
+ const imageRenderer = renderer.image.bind(renderer);
3239
+ renderer.image = image => {
3240
+ image.href = new URL(image.href, baseUrl).toString();
3241
+ const html = imageRenderer(image);
3242
+ return html;
3243
+ };
3244
+ }
3230
3245
  const html = await k(markdown, {
3231
3246
  renderer
3232
3247
  });
@@ -3234,13 +3249,13 @@ const renderMarkdown$1 = async (markdown, options = {}) => {
3234
3249
  };
3235
3250
 
3236
3251
  const commandMap = {
3252
+ 'Markdown.getMarkDownVirtualDom': getMarkdownVirtualDom,
3237
3253
  'Markdown.getVirtualDom': getMarkdownVirtualDom,
3238
3254
  'Markdown.handleMessagePort': handleMessagePort,
3239
3255
  'Markdown.render': renderMarkdown$1,
3240
- 'Markdown.terminate': terminate,
3241
3256
  // deprecated
3242
3257
  'Markdown.renderMarkdown': renderMarkdown$1,
3243
- 'Markdown.getMarkDownVirtualDom': getMarkdownVirtualDom
3258
+ 'Markdown.terminate': terminate
3244
3259
  };
3245
3260
 
3246
3261
  const rpcs = Object.create(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/markdown-worker",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "description": "Markdown Worker",
5
5
  "repository": {
6
6
  "type": "git",