@json-to-office/core-docx 0.37.0 → 0.38.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.
@@ -2104,8 +2104,7 @@ import {
2104
2104
  PageNumber as PageNumber2,
2105
2105
  ColumnBreak,
2106
2106
  TableLayoutType,
2107
- VerticalAlign,
2108
- Bookmark
2107
+ VerticalAlign
2109
2108
  } from "docx";
2110
2109
 
2111
2110
  // src/utils/imageUtils.ts
@@ -3700,6 +3699,11 @@ function getStyleIdForLevel(level) {
3700
3699
 
3701
3700
  // src/utils/bookmarkRegistry.ts
3702
3701
  import { AsyncLocalStorage as AsyncLocalStorage3 } from "async_hooks";
3702
+ import { BookmarkStart, BookmarkEnd } from "docx";
3703
+ var CONTENT_LINK_ID_BASE = 2e6;
3704
+ function createState() {
3705
+ return { bookmarks: /* @__PURE__ */ new Map(), nextLinkId: CONTENT_LINK_ID_BASE + 1 };
3706
+ }
3703
3707
  function slugifyBookmarkText(text) {
3704
3708
  return text.toLowerCase().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "").substring(0, 40);
3705
3709
  }
@@ -3712,14 +3716,14 @@ function dedupeBookmarkId(base, taken) {
3712
3716
  return id;
3713
3717
  }
3714
3718
  var BookmarkRegistry = class {
3715
- fallback = { bookmarks: /* @__PURE__ */ new Map() };
3719
+ fallback = createState();
3716
3720
  scopes = new AsyncLocalStorage3();
3717
3721
  get state() {
3718
3722
  return this.scopes.getStore() ?? this.fallback;
3719
3723
  }
3720
3724
  /** Run work with an isolated registry that follows its async call chain. */
3721
3725
  runScoped(callback) {
3722
- return this.scopes.run({ bookmarks: /* @__PURE__ */ new Map() }, callback);
3726
+ return this.scopes.run(createState(), callback);
3723
3727
  }
3724
3728
  /**
3725
3729
  * Register a bookmark
@@ -3732,6 +3736,19 @@ var BookmarkRegistry = class {
3732
3736
  }
3733
3737
  this.state.bookmarks.set(id, { id, title, type });
3734
3738
  }
3739
+ /**
3740
+ * A `w:id` no other bookmark in this document will use.
3741
+ *
3742
+ * docx's own `Bookmark` cannot supply one: its constructor builds a fresh
3743
+ * id generator per instance (`bookmarkUniqueNumericIdGen()`), so every
3744
+ * bookmark it emits carries `w:id="1"`. Reported upstream as dolanmiu/docx
3745
+ * #3478, still unreleased as of 9.7.1. Duplicated ids leave the start/end
3746
+ * pairing ambiguous, which is why a `REF` field could not read a target's
3747
+ * text even though navigating to it by name worked.
3748
+ */
3749
+ allocateLinkId() {
3750
+ return this.state.nextLinkId++;
3751
+ }
3735
3752
  /**
3736
3753
  * Generate a unique bookmark ID from text
3737
3754
  * Converts text to a URL-friendly format
@@ -3781,6 +3798,14 @@ var BookmarkRegistry = class {
3781
3798
  }
3782
3799
  };
3783
3800
  var globalBookmarkRegistry = new BookmarkRegistry();
3801
+ function createBookmarkedContent(name, children) {
3802
+ const linkId = globalBookmarkRegistry.allocateLinkId();
3803
+ return [
3804
+ new BookmarkStart(name, linkId),
3805
+ ...children,
3806
+ new BookmarkEnd(linkId)
3807
+ ];
3808
+ }
3784
3809
 
3785
3810
  // src/utils/revisionUtils.ts
3786
3811
  import { TextRun as TextRun3, InsertedTextRun, DeletedTextRun } from "docx";
@@ -3949,7 +3974,7 @@ import { Paragraph, TextRun as TextRun4 } from "docx";
3949
3974
  import { AsyncLocalStorage as AsyncLocalStorage5 } from "async_hooks";
3950
3975
  var DEFAULT_COMMENT_AUTHOR = "json-to-office";
3951
3976
  var DEFAULT_COMMENT_DATE = "1970-01-01T00:00:00Z";
3952
- function createState() {
3977
+ function createState2() {
3953
3978
  return { counter: 0, comments: [], hasResolved: false };
3954
3979
  }
3955
3980
  function deriveInitials(author) {
@@ -3960,14 +3985,14 @@ function bodyParagraphs(text) {
3960
3985
  return normalizeUnicodeText(text).split("\n").map((line) => new Paragraph({ children: [new TextRun4({ text: line })] }));
3961
3986
  }
3962
3987
  var CommentRegistry = class {
3963
- fallback = createState();
3988
+ fallback = createState2();
3964
3989
  scopes = new AsyncLocalStorage5();
3965
3990
  get state() {
3966
3991
  return this.scopes.getStore() ?? this.fallback;
3967
3992
  }
3968
3993
  /** Run work with an isolated registry that follows its async call chain. */
3969
3994
  runScoped(callback) {
3970
- return this.scopes.run(createState(), callback);
3995
+ return this.scopes.run(createState2(), callback);
3971
3996
  }
3972
3997
  /**
3973
3998
  * Register a comment thread and return every id its anchors must carry — the
@@ -4035,7 +4060,7 @@ var CommentRegistry = class {
4035
4060
  state.comments.length = 0;
4036
4061
  state.hasResolved = false;
4037
4062
  } else {
4038
- this.fallback = createState();
4063
+ this.fallback = createState2();
4039
4064
  }
4040
4065
  }
4041
4066
  };
@@ -4057,7 +4082,7 @@ function closeCommentRange(ids) {
4057
4082
  // src/utils/noteRegistry.ts
4058
4083
  import { Paragraph as Paragraph2, TextRun as TextRun6 } from "docx";
4059
4084
  import { AsyncLocalStorage as AsyncLocalStorage6 } from "async_hooks";
4060
- function createState2() {
4085
+ function createState3() {
4061
4086
  return {
4062
4087
  footnoteCounter: 0,
4063
4088
  endnoteCounter: 0,
@@ -4074,14 +4099,14 @@ function bodyParagraphs2(text, style) {
4074
4099
  );
4075
4100
  }
4076
4101
  var NoteRegistry = class {
4077
- fallback = createState2();
4102
+ fallback = createState3();
4078
4103
  scopes = new AsyncLocalStorage6();
4079
4104
  get state() {
4080
4105
  return this.scopes.getStore() ?? this.fallback;
4081
4106
  }
4082
4107
  /** Run work with an isolated registry that follows its async call chain. */
4083
4108
  runScoped(callback) {
4084
- return this.scopes.run(createState2(), callback);
4109
+ return this.scopes.run(createState3(), callback);
4085
4110
  }
4086
4111
  /**
4087
4112
  * Register a footnote body and return the id its reference must use. Ids are
@@ -4125,7 +4150,7 @@ var NoteRegistry = class {
4125
4150
  delete state.endnotes[key];
4126
4151
  }
4127
4152
  } else {
4128
- this.fallback = createState2();
4153
+ this.fallback = createState3();
4129
4154
  }
4130
4155
  }
4131
4156
  };
@@ -4291,10 +4316,7 @@ function createText(content, theme, themeName, options = {}) {
4291
4316
  "paragraph"
4292
4317
  );
4293
4318
  children.push(
4294
- new Bookmark({
4295
- id: options.bookmarkId,
4296
- children: revisionRuns
4297
- })
4319
+ ...createBookmarkedContent(options.bookmarkId, revisionRuns)
4298
4320
  );
4299
4321
  } else {
4300
4322
  children.push(...revisionRuns);
@@ -4317,12 +4339,7 @@ function createText(content, theme, themeName, options = {}) {
4317
4339
  normalizedContent,
4318
4340
  "paragraph"
4319
4341
  );
4320
- children.push(
4321
- new Bookmark({
4322
- id: options.bookmarkId,
4323
- children: textRuns
4324
- })
4325
- );
4342
+ children.push(...createBookmarkedContent(options.bookmarkId, textRuns));
4326
4343
  } else {
4327
4344
  children.push(...textRuns);
4328
4345
  }
@@ -4483,10 +4500,7 @@ function createHeading(text, level, theme, _themeName, options = {}) {
4483
4500
  "heading"
4484
4501
  );
4485
4502
  children.push(
4486
- new Bookmark({
4487
- id: options.bookmarkId,
4488
- children: revisionRuns
4489
- })
4503
+ ...createBookmarkedContent(options.bookmarkId, revisionRuns)
4490
4504
  );
4491
4505
  } else {
4492
4506
  children.push(...revisionRuns);
@@ -4509,10 +4523,7 @@ function createHeading(text, level, theme, _themeName, options = {}) {
4509
4523
  headingTextChildren.push(...makeHeadingRuns(normalizedText));
4510
4524
  }
4511
4525
  children.push(
4512
- new Bookmark({
4513
- id: options.bookmarkId,
4514
- children: headingTextChildren
4515
- })
4526
+ ...createBookmarkedContent(options.bookmarkId, headingTextChildren)
4516
4527
  );
4517
4528
  } else {
4518
4529
  if (hasInlineSyntax) {
@@ -4704,9 +4715,7 @@ function createList(items, _theme, _themeName, options = {}) {
4704
4715
  let itemContent = textRuns;
4705
4716
  if (itemId) {
4706
4717
  globalBookmarkRegistry.register(itemId, itemText, "list-item");
4707
- itemContent = [
4708
- new Bookmark({ id: itemId, children: textRuns })
4709
- ];
4718
+ itemContent = createBookmarkedContent(itemId, textRuns);
4710
4719
  }
4711
4720
  const paragraphChildren = [
4712
4721
  ...commentAnchor && index === firstRendered ? commentAnchor.start : [],
@@ -6701,8 +6710,8 @@ import {
6701
6710
  Paragraph as Paragraph9,
6702
6711
  TextRun as TextRun9,
6703
6712
  AlignmentType as AlignmentType5,
6704
- BookmarkStart as BookmarkStart2,
6705
- BookmarkEnd as BookmarkEnd2
6713
+ BookmarkStart as BookmarkStart3,
6714
+ BookmarkEnd as BookmarkEnd3
6706
6715
  } from "docx";
6707
6716
  init_defaults();
6708
6717
  init_styleHelpers();
@@ -7911,23 +7920,23 @@ async function renderTableComponent(component, theme, themeName) {
7911
7920
  }
7912
7921
 
7913
7922
  // src/components/section.ts
7914
- import { Paragraph as Paragraph6, BookmarkStart, BookmarkEnd } from "docx";
7923
+ import { Paragraph as Paragraph6, BookmarkStart as BookmarkStart2, BookmarkEnd as BookmarkEnd2 } from "docx";
7915
7924
 
7916
7925
  // src/core/sectionBookmarks.ts
7917
7926
  import { AsyncLocalStorage as AsyncLocalStorage8 } from "async_hooks";
7918
7927
  var NESTED_LINK_ID_BASE = 1e6;
7919
- function createState3() {
7928
+ function createState4() {
7920
7929
  return { nextNested: 1, resolved: /* @__PURE__ */ new WeakMap() };
7921
7930
  }
7922
7931
  var SectionBookmarkRegistry = class {
7923
- fallback = createState3();
7932
+ fallback = createState4();
7924
7933
  scopes = new AsyncLocalStorage8();
7925
7934
  get state() {
7926
7935
  return this.scopes.getStore() ?? this.fallback;
7927
7936
  }
7928
7937
  /** Run work with an isolated registry that follows its async call chain. */
7929
7938
  runScoped(callback) {
7930
- return this.scopes.run(createState3(), callback);
7939
+ return this.scopes.run(createState4(), callback);
7931
7940
  }
7932
7941
  /**
7933
7942
  * The bookmark for a layout section, derived from its ordinal.
@@ -7958,7 +7967,7 @@ var SectionBookmarkRegistry = class {
7958
7967
  /** Test-only: reset the unscoped fallback counters. */
7959
7968
  clear() {
7960
7969
  if (!this.scopes.getStore()) {
7961
- this.fallback = createState3();
7970
+ this.fallback = createState4();
7962
7971
  }
7963
7972
  }
7964
7973
  };
@@ -7971,7 +7980,7 @@ async function renderSectionComponent(component, theme, themeName, context) {
7971
7980
  const { id: sectionBookmarkId, linkId: bookmarkLinkId } = globalSectionBookmarkRegistry.forSectionComponent(component);
7972
7981
  elements.push(
7973
7982
  new Paragraph6({
7974
- children: [new BookmarkStart(sectionBookmarkId, bookmarkLinkId)],
7983
+ children: [new BookmarkStart2(sectionBookmarkId, bookmarkLinkId)],
7975
7984
  spacing: {
7976
7985
  before: 0,
7977
7986
  after: 0,
@@ -7999,7 +8008,7 @@ async function renderSectionComponent(component, theme, themeName, context) {
7999
8008
  }
8000
8009
  elements.push(
8001
8010
  new Paragraph6({
8002
- children: [new BookmarkEnd(bookmarkLinkId)],
8011
+ children: [new BookmarkEnd2(bookmarkLinkId)],
8003
8012
  spacing: {
8004
8013
  before: 0,
8005
8014
  after: 0,
@@ -9036,7 +9045,7 @@ async function renderSection(section, theme, themeName, context, sectionOrdinal,
9036
9045
  if (sectionBookmarkId && isFirstLayoutOfUserSection && sharedLinkId !== void 0) {
9037
9046
  elements.push(
9038
9047
  new Paragraph9({
9039
- children: [new BookmarkStart2(sectionBookmarkId, sharedLinkId)],
9048
+ children: [new BookmarkStart3(sectionBookmarkId, sharedLinkId)],
9040
9049
  spacing: {
9041
9050
  before: 0,
9042
9051
  after: 0,
@@ -9061,7 +9070,7 @@ async function renderSection(section, theme, themeName, context, sectionOrdinal,
9061
9070
  if (closeBookmark && sharedLinkId !== void 0) {
9062
9071
  elements.push(
9063
9072
  new Paragraph9({
9064
- children: [new BookmarkEnd2(sharedLinkId)]
9073
+ children: [new BookmarkEnd3(sharedLinkId)]
9065
9074
  })
9066
9075
  );
9067
9076
  }