@mtcute/markdown-parser 0.20.0 → 0.22.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.
Files changed (3) hide show
  1. package/index.cjs +54 -10
  2. package/index.js +54 -10
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -228,16 +228,18 @@ function parse(strings, ...sub) {
228
228
  }
229
229
  pos += 1;
230
230
  if (pos > text.length) {
231
- throw new Error("Malformed PRE entity, expected LF after ```");
231
+ result += "```";
232
+ feed(language);
233
+ } else {
234
+ if (!("pre" in stacks)) stacks.pre = [];
235
+ stacks.pre.push({
236
+ _: "messageEntityPre",
237
+ offset: result.length,
238
+ length: 0,
239
+ language
240
+ });
241
+ insidePre = true;
232
242
  }
233
- if (!("pre" in stacks)) stacks.pre = [];
234
- stacks.pre.push({
235
- _: "messageEntityPre",
236
- offset: result.length,
237
- length: 0,
238
- language
239
- });
240
- insidePre = true;
241
243
  } else {
242
244
  pos += 1;
243
245
  if (!("code" in stacks)) stacks.code = [];
@@ -335,9 +337,51 @@ function parse(strings, ...sub) {
335
337
  }
336
338
  });
337
339
  feed(strings[strings.length - 1]);
340
+ function adjustOffsets(from, by) {
341
+ for (const ent of entities) {
342
+ if (ent.offset >= from) ent.offset += by;
343
+ }
344
+ for (const stack of Object.values(stacks)) {
345
+ for (const ent of stack) {
346
+ if (ent.offset >= from) ent.offset += by;
347
+ }
348
+ }
349
+ }
338
350
  for (const [name, stack] of Object.entries(stacks)) {
351
+ if (stack.length > 1) {
352
+ throw new Error(`Malformed ${name} entity`);
353
+ }
339
354
  if (stack.length) {
340
- throw new Error(`Unterminated ${name} entity`);
355
+ switch (name) {
356
+ case "link": {
357
+ const startOffset = stack.pop().offset;
358
+ insideLink = false;
359
+ insideLinkUrl = false;
360
+ const url = pendingLinkUrl;
361
+ pendingLinkUrl = "";
362
+ result = `${result.substring(0, startOffset)}[${result.substring(startOffset)}](`;
363
+ adjustOffsets(startOffset, 1);
364
+ feed(url);
365
+ break;
366
+ }
367
+ default: {
368
+ const startOffset = stack.pop().offset;
369
+ const tag = {
370
+ Bold: TAG_BOLD,
371
+ Italic: TAG_ITALIC,
372
+ Underline: TAG_UNDERLINE,
373
+ Strike: TAG_STRIKE,
374
+ Spoiler: TAG_SPOILER,
375
+ code: TAG_CODE
376
+ }[name];
377
+ if (!tag) throw new Error(`invalid tag ${name}`);
378
+ const remaining = result.substring(startOffset);
379
+ result = `${result.substring(0, startOffset)}${tag}`;
380
+ adjustOffsets(startOffset, tag.length);
381
+ feed(remaining);
382
+ break;
383
+ }
384
+ }
341
385
  }
342
386
  }
343
387
  return {
package/index.js CHANGED
@@ -221,16 +221,18 @@ function parse(strings, ...sub) {
221
221
  }
222
222
  pos += 1;
223
223
  if (pos > text.length) {
224
- throw new Error("Malformed PRE entity, expected LF after ```");
224
+ result += "```";
225
+ feed(language);
226
+ } else {
227
+ if (!("pre" in stacks)) stacks.pre = [];
228
+ stacks.pre.push({
229
+ _: "messageEntityPre",
230
+ offset: result.length,
231
+ length: 0,
232
+ language
233
+ });
234
+ insidePre = true;
225
235
  }
226
- if (!("pre" in stacks)) stacks.pre = [];
227
- stacks.pre.push({
228
- _: "messageEntityPre",
229
- offset: result.length,
230
- length: 0,
231
- language
232
- });
233
- insidePre = true;
234
236
  } else {
235
237
  pos += 1;
236
238
  if (!("code" in stacks)) stacks.code = [];
@@ -328,9 +330,51 @@ function parse(strings, ...sub) {
328
330
  }
329
331
  });
330
332
  feed(strings[strings.length - 1]);
333
+ function adjustOffsets(from, by) {
334
+ for (const ent of entities) {
335
+ if (ent.offset >= from) ent.offset += by;
336
+ }
337
+ for (const stack of Object.values(stacks)) {
338
+ for (const ent of stack) {
339
+ if (ent.offset >= from) ent.offset += by;
340
+ }
341
+ }
342
+ }
331
343
  for (const [name, stack] of Object.entries(stacks)) {
344
+ if (stack.length > 1) {
345
+ throw new Error(`Malformed ${name} entity`);
346
+ }
332
347
  if (stack.length) {
333
- throw new Error(`Unterminated ${name} entity`);
348
+ switch (name) {
349
+ case "link": {
350
+ const startOffset = stack.pop().offset;
351
+ insideLink = false;
352
+ insideLinkUrl = false;
353
+ const url = pendingLinkUrl;
354
+ pendingLinkUrl = "";
355
+ result = `${result.substring(0, startOffset)}[${result.substring(startOffset)}](`;
356
+ adjustOffsets(startOffset, 1);
357
+ feed(url);
358
+ break;
359
+ }
360
+ default: {
361
+ const startOffset = stack.pop().offset;
362
+ const tag = {
363
+ Bold: TAG_BOLD,
364
+ Italic: TAG_ITALIC,
365
+ Underline: TAG_UNDERLINE,
366
+ Strike: TAG_STRIKE,
367
+ Spoiler: TAG_SPOILER,
368
+ code: TAG_CODE
369
+ }[name];
370
+ if (!tag) throw new Error(`invalid tag ${name}`);
371
+ const remaining = result.substring(startOffset);
372
+ result = `${result.substring(0, startOffset)}${tag}`;
373
+ adjustOffsets(startOffset, tag.length);
374
+ feed(remaining);
375
+ break;
376
+ }
377
+ }
334
378
  }
335
379
  }
336
380
  return {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@mtcute/markdown-parser",
3
3
  "type": "module",
4
- "version": "0.20.0",
4
+ "version": "0.22.0",
5
5
  "description": "Markdown entities parser for mtcute",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
- "@mtcute/core": "^0.20.0",
8
+ "@mtcute/core": "^0.22.0",
9
9
  "long": "5.2.3"
10
10
  },
11
11
  "exports": {