@repo-toolkit/confluence 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 (5) hide show
  1. package/README.md +90 -12
  2. package/cli.js +556 -6
  3. package/index.d.ts +130 -1
  4. package/index.js +540 -4
  5. package/package.json +2 -2
package/index.js CHANGED
@@ -14,6 +14,7 @@ var DEFAULT_USER_AGENT = "repo-toolkit-confluence/1.0 (+node)";
14
14
  var MAX_LIMIT = 250;
15
15
  var MAX_ERROR_BODY_LENGTH = 8192;
16
16
  var MAX_PAGES_PER_QUERY = 100;
17
+ var CONFLUENCE_MANAGED_LABEL = "repo-toolkit-confluence";
17
18
  var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
18
19
  var DEFAULT_MAX_RETRIES = 3;
19
20
  var DEFAULT_MAX_UPLOAD_BYTES = 50 * 1024 * 1024;
@@ -141,6 +142,44 @@ var ConfluenceClient = class {
141
142
  body: JSON.stringify(body)
142
143
  });
143
144
  }
145
+ async getPageDescendants(pageId) {
146
+ const query = new URLSearchParams({ limit: String(MAX_LIMIT) });
147
+ return this.listAll(this.v2Url(`/pages/${encodeURIComponent(pageId)}/descendants?${query.toString()}`));
148
+ }
149
+ async getPageLabels(pageId) {
150
+ const query = new URLSearchParams({ limit: String(MAX_LIMIT) });
151
+ return this.listAll(this.v2Url(`/pages/${encodeURIComponent(pageId)}/labels?${query.toString()}`));
152
+ }
153
+ async addManagedLabel(pageId) {
154
+ await this.requestJson(this.v1Url(`/content/${encodeURIComponent(pageId)}/label`), {
155
+ method: "POST",
156
+ headers: { "Content-Type": "application/json" },
157
+ body: JSON.stringify([{ prefix: "global", name: CONFLUENCE_MANAGED_LABEL }])
158
+ });
159
+ }
160
+ async deletePage(pageId) {
161
+ await this.requestJson(this.v2Url(`/pages/${encodeURIComponent(pageId)}`), { method: "DELETE" });
162
+ }
163
+ async listAll(startUrl) {
164
+ const results = [];
165
+ const visited = /* @__PURE__ */ new Set();
166
+ let pageCount = 0;
167
+ let nextUrl = startUrl;
168
+ while (nextUrl) {
169
+ pageCount += 1;
170
+ if (pageCount > MAX_PAGES_PER_QUERY) {
171
+ throw new ConfluenceApiError(`Pagination limit (${MAX_PAGES_PER_QUERY}) exceeded`, 0, nextUrl, "");
172
+ }
173
+ if (visited.has(nextUrl)) {
174
+ throw new ConfluenceApiError("Confluence pagination loop detected", 0, nextUrl, "");
175
+ }
176
+ visited.add(nextUrl);
177
+ const data = await this.requestJson(nextUrl, { method: "GET" });
178
+ results.push(...data.results);
179
+ nextUrl = resolveNextUrl(this.baseUrl, this.baseUrlOrigin, data._links?.next);
180
+ }
181
+ return results;
182
+ }
144
183
  async getAttachments(pageId) {
145
184
  const results = [];
146
185
  const visited = /* @__PURE__ */ new Set();
@@ -1647,6 +1686,133 @@ function runMmdc(cmdPath, source, outFile, timeoutMs, maxStreamBytes) {
1647
1686
  });
1648
1687
  }
1649
1688
 
1689
+ // src/parent-summary.ts
1690
+ var PARENT_SUMMARY_START_MARKER = "<!-- repo-toolkit-confluence:parent-summary:start -->";
1691
+ var PARENT_SUMMARY_END_MARKER = "<!-- repo-toolkit-confluence:parent-summary:end -->";
1692
+ function escapeCdata(text) {
1693
+ return text.replace(/]]>/g, "]]]]><![CDATA[>");
1694
+ }
1695
+ function pageLink(pageId, title) {
1696
+ const safeTitle = escapeCdata(title);
1697
+ return `<ac:link><ri:page ri:content-id="${escapeXmlAttribute(pageId)}" /><ac:plain-text-link-body><![CDATA[${safeTitle}]]></ac:plain-text-link-body></ac:link>`;
1698
+ }
1699
+ function countOccurrences(haystack, needle) {
1700
+ let count = 0;
1701
+ let idx = 0;
1702
+ while (true) {
1703
+ const found = haystack.indexOf(needle, idx);
1704
+ if (found === -1) {
1705
+ break;
1706
+ }
1707
+ count += 1;
1708
+ idx = found + needle.length;
1709
+ }
1710
+ return count;
1711
+ }
1712
+ function mergeParentSummaryBody(currentBody, generatedRegion) {
1713
+ const startCount = countOccurrences(currentBody, PARENT_SUMMARY_START_MARKER);
1714
+ const endCount = countOccurrences(currentBody, PARENT_SUMMARY_END_MARKER);
1715
+ if (startCount === 0 && endCount === 0) {
1716
+ if (currentBody === "") {
1717
+ return generatedRegion;
1718
+ }
1719
+ const sep2 = currentBody.endsWith("\n") ? "" : "\n";
1720
+ return currentBody + sep2 + generatedRegion;
1721
+ }
1722
+ if (startCount === 1 && endCount === 1) {
1723
+ const startIdx = currentBody.indexOf(PARENT_SUMMARY_START_MARKER);
1724
+ const endIdx = currentBody.indexOf(PARENT_SUMMARY_END_MARKER);
1725
+ if (startIdx === -1 || endIdx === -1) {
1726
+ throw new Error("malformed parent summary markers: missing marker");
1727
+ }
1728
+ if (startIdx > endIdx) {
1729
+ throw new Error("malformed parent summary markers: start after end");
1730
+ }
1731
+ const before = currentBody.slice(0, startIdx);
1732
+ const after = currentBody.slice(endIdx + PARENT_SUMMARY_END_MARKER.length);
1733
+ if (countOccurrences(before, PARENT_SUMMARY_START_MARKER) !== 0 || countOccurrences(before, PARENT_SUMMARY_END_MARKER) !== 0) {
1734
+ throw new Error("malformed parent summary markers: duplicate marker before region");
1735
+ }
1736
+ if (countOccurrences(after, PARENT_SUMMARY_START_MARKER) !== 0 || countOccurrences(after, PARENT_SUMMARY_END_MARKER) !== 0) {
1737
+ throw new Error("malformed parent summary markers: duplicate marker after region");
1738
+ }
1739
+ return before + generatedRegion + after;
1740
+ }
1741
+ throw new Error("malformed or duplicate parent summary markers: expected 0 or 1 managed region");
1742
+ }
1743
+ function renderParentSummary(input) {
1744
+ const lines = [];
1745
+ lines.push(PARENT_SUMMARY_START_MARKER);
1746
+ lines.push("<h2>Synced documentation</h2>");
1747
+ if (input.repositoryUrl) {
1748
+ const url = escapeXmlAttribute(input.repositoryUrl);
1749
+ const text = escapeHtml(input.repositoryUrl);
1750
+ lines.push(`<p><em>This documentation subtree is synced from <a href="${url}">${text}</a>.</em></p>`);
1751
+ }
1752
+ lines.push("<h3>Statistics</h3>");
1753
+ lines.push("<ul>");
1754
+ lines.push(`<li>Markdown pages: ${input.stats.markdownPages}</li>`);
1755
+ lines.push(`<li>Directory pages: ${input.stats.directoryPages}</li>`);
1756
+ lines.push(`<li>Total managed pages: ${input.stats.totalPages}</li>`);
1757
+ lines.push(`<li>Maximum depth: ${input.stats.maxDepth}</li>`);
1758
+ lines.push(`<li>Attachment references: ${input.stats.attachmentReferences}</li>`);
1759
+ lines.push(`<li>Mermaid blocks: ${input.stats.mermaidBlocks}</li>`);
1760
+ lines.push("</ul>");
1761
+ lines.push("<h3>Pages</h3>");
1762
+ if (input.pages.length === 0) {
1763
+ lines.push("<p>No managed child pages</p>");
1764
+ } else {
1765
+ lines.push(renderTree(input.pages));
1766
+ }
1767
+ lines.push(PARENT_SUMMARY_END_MARKER);
1768
+ return lines.join("\n");
1769
+ }
1770
+ function renderTree(pages) {
1771
+ const sorted = [...pages].sort((a, b) => a.relativePath.localeCompare(b.relativePath));
1772
+ const root = { children: /* @__PURE__ */ new Map(), key: "" };
1773
+ for (const page of sorted) {
1774
+ const parts = page.relativePath.split("/");
1775
+ let node = root;
1776
+ let currentPath = "";
1777
+ for (let i = 0; i < parts.length; i += 1) {
1778
+ const part = parts[i] ?? "";
1779
+ currentPath = currentPath ? currentPath + "/" + part : part;
1780
+ let child = node.children.get(part);
1781
+ if (!child) {
1782
+ child = { children: /* @__PURE__ */ new Map(), key: part };
1783
+ node.children.set(part, child);
1784
+ }
1785
+ if (i === parts.length - 1) {
1786
+ child.record = page;
1787
+ }
1788
+ node = child;
1789
+ }
1790
+ }
1791
+ const renderNode = (node) => {
1792
+ const entries = [...node.children.entries()].sort((a, b) => a[0].localeCompare(b[0]));
1793
+ if (entries.length === 0) {
1794
+ return "";
1795
+ }
1796
+ let html = "<ul>";
1797
+ for (const [, child] of entries) {
1798
+ const rec = child.record;
1799
+ if (rec) {
1800
+ const link = pageLink(rec.pageId, rec.title);
1801
+ const kindLabel = rec.kind === "directory" ? "directory" : "page";
1802
+ const pathCode = `<code>${escapeHtml(rec.relativePath)}</code>`;
1803
+ const childrenHtml = renderNode(child);
1804
+ html += `<li>${link} ${pathCode} <em>(${kindLabel})</em>${childrenHtml}</li>`;
1805
+ } else {
1806
+ const childrenHtml = renderNode(child);
1807
+ html += `<li>${escapeHtml(child.key)}${childrenHtml}</li>`;
1808
+ }
1809
+ }
1810
+ html += "</ul>";
1811
+ return html;
1812
+ };
1813
+ return renderNode(root);
1814
+ }
1815
+
1650
1816
  // src/index.ts
1651
1817
  var INTERACTIVE_FLAG = { name: "interactive", aliases: ["i"], boolean: true };
1652
1818
  var LocalSyncValidationAggregateError = class extends Error {
@@ -1705,6 +1871,129 @@ var SyncMutationError = class extends Error {
1705
1871
  this.unprocessed = input.unprocessed;
1706
1872
  }
1707
1873
  };
1874
+ var ReconciliationError = class extends Error {
1875
+ constructor(input) {
1876
+ super(input.failure.error.message);
1877
+ this.name = "ReconciliationError";
1878
+ this.phase = input.phase;
1879
+ this.completed = input.completed;
1880
+ this.failure = input.failure;
1881
+ this.unprocessed = input.unprocessed;
1882
+ }
1883
+ };
1884
+ var ParentSummaryError = class extends Error {
1885
+ constructor(input) {
1886
+ super(input.failure.error.message);
1887
+ this.name = "ParentSummaryError";
1888
+ this.phase = "parent-summary";
1889
+ this.changes = input.changes;
1890
+ this.labelsAdded = input.labelsAdded;
1891
+ this.cleanDeletions = input.cleanDeletions;
1892
+ this.pruneDeletions = input.pruneDeletions;
1893
+ this.blocked = input.blocked;
1894
+ this.failure = input.failure;
1895
+ }
1896
+ };
1897
+ function planStalePruning(input) {
1898
+ const nodes = buildInventoryNodes(input.parentPageId, input.inventory);
1899
+ const retained = /* @__PURE__ */ new Set();
1900
+ const visited = /* @__PURE__ */ new Set();
1901
+ const isStale = (entry) => entry.type === "page" && entry.labeled && !input.expectedIds.has(entry.id) && entry.id !== input.parentPageId;
1902
+ const visit = (node) => {
1903
+ if (visited.has(node.entry.id)) {
1904
+ return;
1905
+ }
1906
+ visited.add(node.entry.id);
1907
+ let safe = isStale(node.entry);
1908
+ for (const child of node.children) {
1909
+ visit(child);
1910
+ if (retained.has(child.entry.id)) {
1911
+ safe = false;
1912
+ }
1913
+ }
1914
+ if (!safe) {
1915
+ retained.add(node.entry.id);
1916
+ }
1917
+ };
1918
+ for (const node of nodes.values()) {
1919
+ visit(node);
1920
+ }
1921
+ const deletions = deepestFirst([...nodes.values()].filter((n) => !retained.has(n.entry.id)));
1922
+ const blocked = [...nodes.values()].filter((n) => retained.has(n.entry.id) && isStale(n.entry)).map((n) => n.entry.id).sort();
1923
+ return { deletions, blocked };
1924
+ }
1925
+ function planCleanDeletions(input) {
1926
+ const nodes = buildInventoryNodes(input.parentPageId, input.inventory);
1927
+ for (const node of nodes.values()) {
1928
+ if (node.entry.type !== "page") {
1929
+ throw new Error(
1930
+ `clean refused: descendant ${node.entry.id} has unsupported type "${node.entry.type}"; deleting its ancestors could remove content that cannot be restored by this tool`
1931
+ );
1932
+ }
1933
+ }
1934
+ return { deletions: deepestFirst([...nodes.values()]), blocked: [] };
1935
+ }
1936
+ function buildInventoryNodes(parentPageId, inventory) {
1937
+ const nodes = /* @__PURE__ */ new Map();
1938
+ for (const entry of inventory) {
1939
+ if (entry.id === parentPageId) {
1940
+ continue;
1941
+ }
1942
+ if (nodes.has(entry.id)) {
1943
+ throw new Error(`Incomplete descendant inventory: duplicate id ${entry.id}`);
1944
+ }
1945
+ nodes.set(entry.id, { entry, depth: -1, children: [] });
1946
+ }
1947
+ const depthOf = (node) => {
1948
+ if (node.depth >= 0) {
1949
+ return node.depth;
1950
+ }
1951
+ if (typeof node.entry.depth === "number") {
1952
+ node.depth = node.entry.depth;
1953
+ return node.depth;
1954
+ }
1955
+ let depth = 0;
1956
+ let current = node;
1957
+ const seen = /* @__PURE__ */ new Set([node.entry.id]);
1958
+ while (true) {
1959
+ const pid = current.entry.parentId;
1960
+ if (pid === parentPageId) {
1961
+ depth += 1;
1962
+ node.depth = depth;
1963
+ return depth;
1964
+ }
1965
+ if (!pid) {
1966
+ throw new Error(`Incomplete descendant inventory: missing parent for ${node.entry.id}`);
1967
+ }
1968
+ if (seen.has(pid)) {
1969
+ throw new Error(`Incomplete descendant inventory: parent cycle at ${pid}`);
1970
+ }
1971
+ seen.add(pid);
1972
+ const parent = nodes.get(pid);
1973
+ if (!parent) {
1974
+ throw new Error(
1975
+ `Incomplete descendant inventory: ancestor ${pid} of ${node.entry.id} is missing from the listing`
1976
+ );
1977
+ }
1978
+ depth += 1;
1979
+ current = parent;
1980
+ }
1981
+ };
1982
+ for (const node of nodes.values()) {
1983
+ depthOf(node);
1984
+ const pid = node.entry.parentId;
1985
+ if (pid !== void 0 && pid !== parentPageId) {
1986
+ nodes.get(pid)?.children.push(node);
1987
+ }
1988
+ }
1989
+ return nodes;
1990
+ }
1991
+ function deepestFirst(nodes) {
1992
+ return [...nodes].sort((a, b) => b.depth - a.depth !== 0 ? b.depth - a.depth : a.entry.id.localeCompare(b.entry.id)).map((n) => n.entry.id);
1993
+ }
1994
+ function hasManagedMarker(labels) {
1995
+ return labels.some((label) => label.name === CONFLUENCE_MANAGED_LABEL && label.prefix === "global");
1996
+ }
1708
1997
  function resolveConfluenceSyncPlan(options = {}) {
1709
1998
  const pageTitleStrategy = resolvePageTitleStrategy(options.pageTitleStrategy);
1710
1999
  const cwd = resolve2(options.cwd ?? process.cwd());
@@ -1754,6 +2043,8 @@ function resolveConfluenceSyncPlan(options = {}) {
1754
2043
  dryRun: options.dryRun ?? false,
1755
2044
  renderHtmlBlocks: options.renderHtmlBlocks === true,
1756
2045
  repositoryUrl,
2046
+ clean: options.clean ?? false,
2047
+ updateParentPage: options.updateParentPage ?? true,
1757
2048
  pageTitleStrategy
1758
2049
  };
1759
2050
  }
@@ -1763,7 +2054,6 @@ async function syncConfluenceToDocs(options = {}) {
1763
2054
  const tree = await readDocTree(plan.folder);
1764
2055
  if (tree.entries.length === 0) {
1765
2056
  log(`No markdown files found under ${plan.folder}`);
1766
- return;
1767
2057
  }
1768
2058
  const localPlan = validateLocalSync(tree.entries, plan);
1769
2059
  validateLocalHierarchy(localPlan.entries, plan.pageTitleStrategy);
@@ -1776,6 +2066,32 @@ async function syncConfluenceToDocs(options = {}) {
1776
2066
  `[dry-run] would sync ${entryPlan.entry.segments.join("/")} as "${entryPlan.title}"` + (attCount > 0 ? ` (${attCount} attachment${attCount === 1 ? "" : "s"} validated)` : "") + (mermaidCount > 0 ? ` (${mermaidCount} mermaid block${mermaidCount === 1 ? "" : "s"})` : "")
1777
2067
  );
1778
2068
  }
2069
+ if (plan.clean) {
2070
+ log(
2071
+ "[dry-run] clean requested: a real sync would move every page descendant of the target page to trash before recreating the local hierarchy."
2072
+ );
2073
+ }
2074
+ log(
2075
+ "[dry-run] a real sync would label every mapped page with the ownership marker and prune stale labeled descendants."
2076
+ );
2077
+ if (plan.updateParentPage) {
2078
+ const stats = computeDryRunStats(localPlan);
2079
+ log(
2080
+ `[dry-run] parent summary: Markdown pages: ${stats.markdownPages}, Directory pages: ${stats.directoryPages}, Total managed pages: ${stats.totalPages}, Maximum depth: ${stats.maxDepth}, Attachment references: ${stats.attachmentReferences}, Mermaid blocks: ${stats.mermaidBlocks}`
2081
+ );
2082
+ if (localPlan.entries.length === 0) {
2083
+ log("[dry-run] parent tree: No managed child pages");
2084
+ } else {
2085
+ for (const entryPlan of localPlan.entries) {
2086
+ const dirParts = entryPlan.entry.segments.slice(0, -1);
2087
+ for (let i = 0; i < dirParts.length; i += 1) {
2088
+ const dirPath = dirParts.slice(0, i + 1).join("/");
2089
+ log(`[dry-run] parent tree: ${dirPath} (directory) => "${dirParts[i] ?? ""}"`);
2090
+ }
2091
+ log(`[dry-run] parent tree: ${entryPlan.entry.segments.join("/")} (page) => "${entryPlan.title}"`);
2092
+ }
2093
+ }
2094
+ }
1779
2095
  return;
1780
2096
  }
1781
2097
  const client = options.client ?? new ConfluenceClient({
@@ -1783,13 +2099,29 @@ async function syncConfluenceToDocs(options = {}) {
1783
2099
  username: plan.username,
1784
2100
  apiToken: plan.apiToken
1785
2101
  });
2102
+ const labelsAdded = [];
2103
+ const cleanDeletions = [];
2104
+ const pruneDeletions = [];
2105
+ const blocked = [];
2106
+ if (plan.clean) {
2107
+ const descendants = await client.getPageDescendants(plan.parentPageId);
2108
+ const inventory = descendants.filter((d) => d.id !== plan.parentPageId).map((d) => toInventoryEntry(d, false));
2109
+ const cleanPlan = planCleanDeletions({ parentPageId: plan.parentPageId, inventory });
2110
+ await executeDeletions(cleanPlan.deletions, "clean", client, log, cleanDeletions);
2111
+ }
1786
2112
  const spaceId = await client.getSpaceIdByKey(plan.spaceKey);
1787
2113
  const cache = new PageTitleCache(spaceId, client);
2114
+ const syncState = {
2115
+ mappedIds: /* @__PURE__ */ new Set(),
2116
+ ensuredLabels: /* @__PURE__ */ new Set(),
2117
+ labelsAdded,
2118
+ mappedRecords: /* @__PURE__ */ new Map()
2119
+ };
1788
2120
  const changes = [];
1789
2121
  for (let i = 0; i < localPlan.entries.length; i += 1) {
1790
2122
  const entryPlan = localPlan.entries[i];
1791
2123
  try {
1792
- await syncEntry(entryPlan, plan, client, cache, log, changes);
2124
+ await syncEntry(entryPlan, plan, client, cache, log, changes, syncState);
1793
2125
  } catch (error) {
1794
2126
  throw new SyncMutationError({
1795
2127
  changes,
@@ -1798,15 +2130,202 @@ async function syncConfluenceToDocs(options = {}) {
1798
2130
  });
1799
2131
  }
1800
2132
  }
1801
- return { changes };
2133
+ if (!plan.clean) {
2134
+ const descendants = await client.getPageDescendants(plan.parentPageId);
2135
+ const inventory = [];
2136
+ for (const d of descendants) {
2137
+ if (d.id === plan.parentPageId) {
2138
+ continue;
2139
+ }
2140
+ if (d.type !== "page") {
2141
+ inventory.push(toInventoryEntry(d, false));
2142
+ continue;
2143
+ }
2144
+ if (syncState.mappedIds.has(d.id)) {
2145
+ inventory.push(toInventoryEntry(d, true));
2146
+ continue;
2147
+ }
2148
+ const labels = await client.getPageLabels(d.id);
2149
+ inventory.push(toInventoryEntry(d, hasManagedMarker(labels)));
2150
+ }
2151
+ const prunePlan = planStalePruning({
2152
+ parentPageId: plan.parentPageId,
2153
+ expectedIds: syncState.mappedIds,
2154
+ inventory
2155
+ });
2156
+ await executeDeletions(prunePlan.deletions, "prune", client, log, pruneDeletions);
2157
+ for (const pageId of prunePlan.blocked) {
2158
+ blocked.push(pageId);
2159
+ log(`blocked: stale page ${pageId} retained because it has unlabeled, non-page, or expected descendants`);
2160
+ }
2161
+ }
2162
+ let parentStatus = "skipped";
2163
+ if (plan.updateParentPage) {
2164
+ try {
2165
+ const parentPage = await client.getPage(plan.parentPageId);
2166
+ const stats = computeParentStats(localPlan, syncState);
2167
+ const pages = [...syncState.mappedRecords.values()].sort((a, b) => a.relativePath.localeCompare(b.relativePath));
2168
+ const region = renderParentSummary({ repositoryUrl: plan.repositoryUrl, stats, pages });
2169
+ const currentBody = parentPage.body?.storage?.value ?? "";
2170
+ const merged = mergeParentSummaryBody(currentBody, region);
2171
+ if (merged === currentBody) {
2172
+ log(`parent-unchanged: page ${plan.parentPageId}`);
2173
+ parentStatus = "unchanged";
2174
+ } else {
2175
+ await client.updatePage({
2176
+ id: plan.parentPageId,
2177
+ title: parentPage.title,
2178
+ body: { representation: "storage", value: merged },
2179
+ version: { number: (parentPage.version?.number ?? 0) + 1, message: plan.versionMessage }
2180
+ });
2181
+ log(`parent-updated: page ${plan.parentPageId}`);
2182
+ parentStatus = "updated";
2183
+ }
2184
+ } catch (error) {
2185
+ throw new ParentSummaryError({
2186
+ changes,
2187
+ labelsAdded: [...labelsAdded],
2188
+ cleanDeletions: [...cleanDeletions],
2189
+ pruneDeletions: [...pruneDeletions],
2190
+ blocked: [...blocked],
2191
+ failure: { pageId: plan.parentPageId, error: error instanceof Error ? error : new Error(String(error)) }
2192
+ });
2193
+ }
2194
+ }
2195
+ return { changes, labelsAdded, cleanDeletions, pruneDeletions, blocked, parentStatus };
2196
+ }
2197
+ function computeParentStats(localPlan, state) {
2198
+ const dirSet = /* @__PURE__ */ new Set();
2199
+ let maxDepth = 0;
2200
+ let attachmentReferences = 0;
2201
+ let mermaidBlocks = 0;
2202
+ for (const entryPlan of localPlan.entries) {
2203
+ const segs = entryPlan.entry.segments;
2204
+ if (segs.length > maxDepth) {
2205
+ maxDepth = segs.length;
2206
+ }
2207
+ for (let i = 0; i < segs.length - 1; i += 1) {
2208
+ dirSet.add(segs.slice(0, i + 1).join("/"));
2209
+ }
2210
+ attachmentReferences += entryPlan.attachments.length;
2211
+ mermaidBlocks += entryPlan.mermaidBlocks.length;
2212
+ }
2213
+ const markdownPages = localPlan.entries.length;
2214
+ const directoryPages = dirSet.size;
2215
+ const totalPages = state.mappedRecords.size;
2216
+ const effectiveMaxDepth = localPlan.entries.length === 0 ? 0 : maxDepth;
2217
+ return {
2218
+ markdownPages,
2219
+ directoryPages,
2220
+ totalPages: totalPages > 0 ? totalPages : directoryPages + markdownPages,
2221
+ maxDepth: effectiveMaxDepth,
2222
+ attachmentReferences,
2223
+ mermaidBlocks
2224
+ };
2225
+ }
2226
+ function computeDryRunStats(localPlan) {
2227
+ const dirSet = /* @__PURE__ */ new Set();
2228
+ let maxDepth = 0;
2229
+ let attachmentReferences = 0;
2230
+ let mermaidBlocks = 0;
2231
+ for (const entryPlan of localPlan.entries) {
2232
+ const segs = entryPlan.entry.segments;
2233
+ if (segs.length > maxDepth) {
2234
+ maxDepth = segs.length;
2235
+ }
2236
+ for (let i = 0; i < segs.length - 1; i += 1) {
2237
+ dirSet.add(segs.slice(0, i + 1).join("/"));
2238
+ }
2239
+ attachmentReferences += entryPlan.attachments.length;
2240
+ mermaidBlocks += entryPlan.mermaidBlocks.length;
2241
+ }
2242
+ const markdownPages = localPlan.entries.length;
2243
+ const directoryPages = dirSet.size;
2244
+ return {
2245
+ markdownPages,
2246
+ directoryPages,
2247
+ totalPages: directoryPages + markdownPages,
2248
+ maxDepth: markdownPages === 0 ? 0 : maxDepth,
2249
+ attachmentReferences,
2250
+ mermaidBlocks
2251
+ };
2252
+ }
2253
+ function toInventoryEntry(d, labeled) {
2254
+ const entry = { id: d.id, type: d.type, labeled };
2255
+ if (d.parentId !== void 0) {
2256
+ entry.parentId = d.parentId;
2257
+ }
2258
+ if (d.depth !== void 0) {
2259
+ entry.depth = d.depth;
2260
+ }
2261
+ if (d.title !== void 0) {
2262
+ entry.title = d.title;
2263
+ }
2264
+ return entry;
1802
2265
  }
1803
- async function syncEntry(entryPlan, plan, client, cache, log, changes) {
2266
+ async function ensureManagedLabel(pageId, client, state, log) {
2267
+ if (state.ensuredLabels.has(pageId)) {
2268
+ return;
2269
+ }
2270
+ const labels = await client.getPageLabels(pageId);
2271
+ if (!hasManagedMarker(labels)) {
2272
+ await client.addManagedLabel(pageId);
2273
+ state.labelsAdded.push(pageId);
2274
+ log(`labeled: page ${pageId}`);
2275
+ }
2276
+ state.ensuredLabels.add(pageId);
2277
+ }
2278
+ async function executeDeletions(ids, phase, client, log, evidence) {
2279
+ for (let i = 0; i < ids.length; i += 1) {
2280
+ const pageId = ids[i];
2281
+ if (pageId === void 0) {
2282
+ continue;
2283
+ }
2284
+ try {
2285
+ await client.deletePage(pageId);
2286
+ evidence.push(pageId);
2287
+ log(`${phase === "clean" ? "clean" : "pruned"}: trashed page ${pageId}`);
2288
+ } catch (error) {
2289
+ throw new ReconciliationError({
2290
+ phase,
2291
+ completed: [...evidence],
2292
+ failure: { pageId, error: error instanceof Error ? error : new Error(String(error)) },
2293
+ unprocessed: ids.slice(i + 1)
2294
+ });
2295
+ }
2296
+ }
2297
+ }
2298
+ async function syncEntry(entryPlan, plan, client, cache, log, changes, state) {
1804
2299
  const { entry, html: precomputedHtml, mermaidBlocks, markdownDir, hasLocalImages, hasMermaidBlocks } = entryPlan;
1805
2300
  const segments = entry.segments;
1806
2301
  if (segments.length === 0) {
1807
2302
  return;
1808
2303
  }
1809
2304
  let currentParentId = plan.parentPageId;
2305
+ const recordDirectory = (relativePath, title, pageId, depth) => {
2306
+ if (!state.mappedRecords.has(relativePath)) {
2307
+ state.mappedRecords.set(relativePath, {
2308
+ relativePath,
2309
+ kind: "directory",
2310
+ title,
2311
+ pageId,
2312
+ depth,
2313
+ attachmentCount: 0,
2314
+ mermaidCount: 0
2315
+ });
2316
+ }
2317
+ };
2318
+ const recordLeaf = (relativePath, title, pageId, depth) => {
2319
+ state.mappedRecords.set(relativePath, {
2320
+ relativePath,
2321
+ kind: "leaf",
2322
+ title,
2323
+ pageId,
2324
+ depth,
2325
+ attachmentCount: entryPlan.attachments.length,
2326
+ mermaidCount: entryPlan.mermaidBlocks.length
2327
+ });
2328
+ };
1810
2329
  for (let idx = 0; idx < segments.length; idx += 1) {
1811
2330
  const isLast = idx === segments.length - 1;
1812
2331
  const segment = segments[idx] ?? "";
@@ -1820,12 +2339,18 @@ async function syncEntry(entryPlan, plan, client, cache, log, changes) {
1820
2339
  parentId: currentParentId,
1821
2340
  body: { representation: "storage", value: precomputedHtml }
1822
2341
  });
2342
+ state.mappedIds.add(pageId2);
2343
+ await ensureManagedLabel(pageId2, client, state, log);
2344
+ recordLeaf(segments.join("/"), title, pageId2, segments.length);
1823
2345
  log(`created: ${segments.join("/")} (page ${pageId2})`);
1824
2346
  changes.push({ entry, pageId: pageId2, kind: "created" });
1825
2347
  return;
1826
2348
  }
1827
2349
  const existingPage = existing ?? await cache.findOrCreate(title, currentParentId);
1828
2350
  const pageId = existingPage.id;
2351
+ state.mappedIds.add(pageId);
2352
+ await ensureManagedLabel(pageId, client, state, log);
2353
+ recordLeaf(segments.join("/"), title, pageId, segments.length);
1829
2354
  const current = await client.getPage(pageId);
1830
2355
  const currentBody = current.body?.storage?.value ?? "";
1831
2356
  let body = precomputedHtml;
@@ -1872,10 +2397,16 @@ async function syncEntry(entryPlan, plan, client, cache, log, changes) {
1872
2397
  if (isMarkdownName(segment)) {
1873
2398
  const title = pageTitleFromSegments(segments.slice(0, idx + 1), plan.pageTitleStrategy);
1874
2399
  const page2 = await cache.findOrCreate(title, currentParentId);
2400
+ state.mappedIds.add(page2.id);
2401
+ await ensureManagedLabel(page2.id, client, state, log);
2402
+ recordDirectory(segments.slice(0, idx + 1).join("/"), title, page2.id, idx + 1);
1875
2403
  currentParentId = page2.id;
1876
2404
  continue;
1877
2405
  }
1878
2406
  const page = await cache.findOrCreate(segment, currentParentId);
2407
+ state.mappedIds.add(page.id);
2408
+ await ensureManagedLabel(page.id, client, state, log);
2409
+ recordDirectory(segments.slice(0, idx + 1).join("/"), segment, page.id, idx + 1);
1879
2410
  currentParentId = page.id;
1880
2411
  }
1881
2412
  }
@@ -2022,12 +2553,15 @@ function validateLocalHierarchy(entries, strategy) {
2022
2553
  }
2023
2554
  }
2024
2555
  export {
2556
+ CONFLUENCE_MANAGED_LABEL,
2025
2557
  ConfluenceApiError,
2026
2558
  ConfluenceClient,
2027
2559
  DEFAULT_PAGE_TITLE_STRATEGY,
2028
2560
  INTERACTIVE_FLAG,
2029
2561
  LocalSyncValidationAggregateError,
2030
2562
  PAGE_TITLE_STRATEGIES,
2563
+ ParentSummaryError,
2564
+ ReconciliationError,
2031
2565
  SyncMutationError,
2032
2566
  escapeAttachmentFilename,
2033
2567
  escapeXmlAttribute,
@@ -2036,6 +2570,8 @@ export {
2036
2570
  isRemoteUrl,
2037
2571
  markdownToStorage,
2038
2572
  pageTitleFromSegments,
2573
+ planCleanDeletions,
2574
+ planStalePruning,
2039
2575
  preflightImagesToAttachments,
2040
2576
  preflightMermaidBlocks,
2041
2577
  readDocTree,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@repo-toolkit/confluence",
3
3
  "description": "Sync a folder of markdown docs to Confluence pages and attachments (GitHub Action compatible)",
4
- "version": "0.20.0",
4
+ "version": "0.22.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "keywords": [
@@ -27,7 +27,7 @@
27
27
  "node": ">=20"
28
28
  },
29
29
  "dependencies": {
30
- "@repo-toolkit/publish-package": "0.20.0"
30
+ "@repo-toolkit/publish-package": "0.22.0"
31
31
  },
32
32
  "files": [
33
33
  "**/*",