@gmb/bitmark-parser-generator 5.20.0 → 5.21.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/dist/index.cjs CHANGED
@@ -11078,7 +11078,7 @@ var instance2 = new Config();
11078
11078
  // src/generated/package_info.ts
11079
11079
  var PACKAGE_INFO = {
11080
11080
  "name": "@gmb/bitmark-parser-generator",
11081
- "version": "5.20.0",
11081
+ "version": "5.21.0",
11082
11082
  "author": "Get More Brain Ltd",
11083
11083
  "license": "ISC",
11084
11084
  "description": "A bitmark parser and generator using Peggy.js"
@@ -12833,6 +12833,31 @@ var AstWalkerGenerator = class {
12833
12833
  }
12834
12834
  return void 0;
12835
12835
  }
12836
+ getSiblingNodes(route) {
12837
+ const parentNode = this.getParentNode(route);
12838
+ if (!parentNode || !parentNode.value || !Array.isArray(parentNode.value)) {
12839
+ return {};
12840
+ }
12841
+ const siblings = parentNode.value;
12842
+ const index = siblings.findIndex((s) => s === route[route.length - 1]?.value);
12843
+ if (index === -1) {
12844
+ return {};
12845
+ }
12846
+ return {
12847
+ left: index > 0 ? siblings[index - 1] : void 0,
12848
+ right: index < siblings.length - 1 ? siblings[index + 1] : void 0
12849
+ };
12850
+ }
12851
+ isEmptyParagraph(node) {
12852
+ if (!node) return false;
12853
+ if (node.type !== TextNodeType.paragraph) return false;
12854
+ if (!node.content || !Array.isArray(node.content)) return true;
12855
+ for (const contentNode of node.content) {
12856
+ if (contentNode.type !== TextNodeType.text) return false;
12857
+ if (contentNode.text && contentNode.text.trim() !== "") return false;
12858
+ }
12859
+ return true;
12860
+ }
12836
12861
  };
12837
12862
 
12838
12863
  // src/generator/text/TextGenerator.ts
@@ -12958,7 +12983,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
12958
12983
  __publicField(this, "inParagraph", false);
12959
12984
  __publicField(this, "inHeading", false);
12960
12985
  __publicField(this, "inCodeBlock", false);
12961
- __publicField(this, "exitedCodeBlock", false);
12962
12986
  __publicField(this, "inBulletList", false);
12963
12987
  __publicField(this, "inInline", false);
12964
12988
  __publicField(this, "markDepth", 0);
@@ -13047,7 +13071,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
13047
13071
  this.inParagraph = false;
13048
13072
  this.inHeading = false;
13049
13073
  this.inCodeBlock = false;
13050
- this.exitedCodeBlock = false;
13051
13074
  this.inBulletList = false;
13052
13075
  this.inInline = false;
13053
13076
  this.markDepth = 0;
@@ -13113,7 +13136,7 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
13113
13136
  switch (node.type) {
13114
13137
  case TextNodeType.paragraph:
13115
13138
  this.inParagraph = true;
13116
- this.writeParagraph(node);
13139
+ this.writeParagraph(node, route);
13117
13140
  break;
13118
13141
  case TextNodeType.hardBreak:
13119
13142
  this.writeHardBreak(node);
@@ -13170,7 +13193,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
13170
13193
  return false;
13171
13194
  default:
13172
13195
  }
13173
- this.exitedCodeBlock = false;
13174
13196
  }
13175
13197
  handleBetweenNode(node, _left, _right, _route) {
13176
13198
  switch (node.type) {
@@ -13204,7 +13226,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
13204
13226
  this.writeNL();
13205
13227
  this.writeNL();
13206
13228
  this.inCodeBlock = false;
13207
- this.exitedCodeBlock = true;
13208
13229
  break;
13209
13230
  case TextNodeType.noBulletList:
13210
13231
  case TextNodeType.bulletList:
@@ -13568,21 +13589,19 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
13568
13589
  }
13569
13590
  }
13570
13591
  }
13571
- writeParagraph(node) {
13592
+ writeParagraph(node, route) {
13572
13593
  if (this.textFormat === TextFormat.bitmarkText) {
13573
13594
  if (this.inBulletList) return;
13574
- const nodeContentLength = node.content?.length ?? 0;
13575
- if (this.nodeIndex === 0) {
13576
- if (nodeContentLength === 1) {
13577
- const isTextNode = node.content?.[0].type === TextNodeType.text;
13578
- const text = node.content?.[0].text ?? "";
13579
- if (!isTextNode || text !== "") return;
13580
- }
13581
- if (nodeContentLength > 1) return;
13595
+ const siblingNodes = this.getSiblingNodes(route);
13596
+ const prevSiblingNodeType = siblingNodes.left?.type;
13597
+ const inBody = this.textLocation === TextLocation.body;
13598
+ const isEmptyParagraph = this.isEmptyParagraph(node);
13599
+ if (!inBody || !isEmptyParagraph && !(prevSiblingNodeType === TextNodeType.codeBlock || prevSiblingNodeType === TextNodeType.paragraph)) {
13600
+ return;
13582
13601
  }
13583
13602
  this.write("|");
13584
13603
  this.writeNL();
13585
- if (this.exitedCodeBlock) {
13604
+ if (prevSiblingNodeType === TextNodeType.codeBlock) {
13586
13605
  this.writeNL();
13587
13606
  }
13588
13607
  }