@gravitek/confluence-mirror-core 0.2.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.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * OAuth2 token manager for Atlassian service accounts
3
+ * Implements Client Credentials Flow (2LO - 2-legged OAuth)
4
+ */
5
+ /**
6
+ * OAuth2 token response structure from Atlassian
7
+ */
8
+ export interface OAuth2Tokens {
9
+ accessToken: string;
10
+ expiresAt: number;
11
+ tokenType: string;
12
+ scope: string;
13
+ }
14
+ /**
15
+ * Manages OAuth2 token acquisition and refresh using Client Credentials Flow
16
+ * Tokens are valid for 60 minutes and automatically refreshed before expiration
17
+ */
18
+ export declare class OAuth2TokenManager {
19
+ private clientId;
20
+ private clientSecret;
21
+ private tokens;
22
+ private readonly tokenEndpoint;
23
+ constructor(clientId: string, clientSecret: string);
24
+ /**
25
+ * Get a valid access token, refreshing if necessary
26
+ * @returns Valid OAuth2 access token
27
+ */
28
+ getAccessToken(): Promise<string>;
29
+ /**
30
+ * Refresh the OAuth2 access token using Client Credentials Flow
31
+ * Makes a POST request to Atlassian's token endpoint with client credentials
32
+ */
33
+ refreshToken(): Promise<void>;
34
+ /**
35
+ * Check if the current token is expired or about to expire
36
+ * Uses a 1-minute buffer before actual expiry to prevent edge cases
37
+ * @returns true if token needs refresh
38
+ */
39
+ private isTokenExpired;
40
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * OAuth2 token manager for Atlassian service accounts
3
+ * Implements Client Credentials Flow (2LO - 2-legged OAuth)
4
+ */
5
+ /**
6
+ * Manages OAuth2 token acquisition and refresh using Client Credentials Flow
7
+ * Tokens are valid for 60 minutes and automatically refreshed before expiration
8
+ */
9
+ export class OAuth2TokenManager {
10
+ constructor(clientId, clientSecret) {
11
+ this.clientId = clientId;
12
+ this.clientSecret = clientSecret;
13
+ this.tokens = null;
14
+ this.tokenEndpoint = "https://auth.atlassian.com/oauth/token";
15
+ }
16
+ /**
17
+ * Get a valid access token, refreshing if necessary
18
+ * @returns Valid OAuth2 access token
19
+ */
20
+ async getAccessToken() {
21
+ if (!this.tokens || this.isTokenExpired()) {
22
+ await this.refreshToken();
23
+ }
24
+ return this.tokens.accessToken;
25
+ }
26
+ /**
27
+ * Refresh the OAuth2 access token using Client Credentials Flow
28
+ * Makes a POST request to Atlassian's token endpoint with client credentials
29
+ */
30
+ async refreshToken() {
31
+ console.log("[OAuth2] Requesting new access token...");
32
+ const startTime = Date.now();
33
+ const params = new URLSearchParams({
34
+ grant_type: "client_credentials",
35
+ client_id: this.clientId,
36
+ client_secret: this.clientSecret,
37
+ audience: "api.atlassian.com",
38
+ });
39
+ const response = await fetch(this.tokenEndpoint, {
40
+ method: "POST",
41
+ headers: {
42
+ "Content-Type": "application/x-www-form-urlencoded",
43
+ },
44
+ body: params.toString(),
45
+ });
46
+ const elapsed = Date.now() - startTime;
47
+ if (!response.ok) {
48
+ const errorBody = await response.text();
49
+ console.error(`[OAuth2] Token request failed after ${elapsed}ms:`, response.status);
50
+ throw new Error(`OAuth2 token request failed: ${response.status} - ${errorBody}`);
51
+ }
52
+ const data = await response.json();
53
+ console.log(`[OAuth2] Access token received in ${elapsed}ms`);
54
+ console.log(`[OAuth2] Token expires in ${data.expires_in}s, scopes: ${data.scope}`);
55
+ this.tokens = {
56
+ accessToken: data.access_token,
57
+ expiresAt: Date.now() + data.expires_in * 1000,
58
+ tokenType: data.token_type,
59
+ scope: data.scope || "",
60
+ };
61
+ }
62
+ /**
63
+ * Check if the current token is expired or about to expire
64
+ * Uses a 1-minute buffer before actual expiry to prevent edge cases
65
+ * @returns true if token needs refresh
66
+ */
67
+ isTokenExpired() {
68
+ if (!this.tokens)
69
+ return true;
70
+ // Refresh 1 minute before expiry to prevent edge cases
71
+ return Date.now() >= this.tokens.expiresAt - 60000;
72
+ }
73
+ }
@@ -0,0 +1,8 @@
1
+ export { ConfluenceClient, ConfluenceApiError } from "./client/confluence-client";
2
+ export { BasicAuthStrategy, OAuth2Strategy, type AuthStrategy, } from "./client/auth-strategies";
3
+ export { processADFWithMedia } from "./processors/media-processor";
4
+ export { processADFWithTOC } from "./processors/toc-processor";
5
+ export { processADFWithLinks, type EnrichedLinkData, type LinkProcessorOptions, } from "./processors/link-processor";
6
+ export { processADF, extractTextFromADFNodes, generateSlug, } from "./processors/adf-processor";
7
+ export { UrlTransformer } from "./utils/url-transformer";
8
+ export * from "./types";
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ // Main exports for confluence-mirror-core
2
+ // Client and error handling
3
+ export { ConfluenceClient, ConfluenceApiError } from "./client/confluence-client";
4
+ // Authentication strategies
5
+ export { BasicAuthStrategy, OAuth2Strategy, } from "./client/auth-strategies";
6
+ // ADF processors
7
+ export { processADFWithMedia } from "./processors/media-processor";
8
+ export { processADFWithTOC } from "./processors/toc-processor";
9
+ export { processADFWithLinks, } from "./processors/link-processor";
10
+ export { processADF, extractTextFromADFNodes, generateSlug, } from "./processors/adf-processor";
11
+ // Utilities
12
+ export { UrlTransformer } from "./utils/url-transformer";
13
+ // Types
14
+ export * from "./types";
@@ -0,0 +1,4 @@
1
+ import { ADFDocument, ADFNode } from '../types';
2
+ export declare function processADF(document: ADFDocument): ADFDocument;
3
+ export declare function extractTextFromADFNodes(nodes: ADFNode[]): string;
4
+ export declare function generateSlug(text: string, existingSlugs?: Set<string>): string;
@@ -0,0 +1,37 @@
1
+ // Pure ADF processing logic without UI dependencies
2
+ export function processADF(document) {
3
+ // This will contain pure ADF transformation logic
4
+ // Currently just returns the document as-is
5
+ return document;
6
+ }
7
+ // Extract text content from ADF nodes (moved from adf-renderer)
8
+ export function extractTextFromADFNodes(nodes) {
9
+ return nodes.map(node => {
10
+ if (node.type === 'text' && node.text) {
11
+ return node.text;
12
+ }
13
+ if (node.content) {
14
+ return extractTextFromADFNodes(node.content);
15
+ }
16
+ return '';
17
+ }).join('');
18
+ }
19
+ // Generate URL-safe slug from text
20
+ export function generateSlug(text, existingSlugs) {
21
+ let baseSlug = text
22
+ .toLowerCase()
23
+ .replace(/[^a-z0-9\s-]/g, '')
24
+ .replace(/\s+/g, '-')
25
+ .trim();
26
+ if (!existingSlugs) {
27
+ return baseSlug;
28
+ }
29
+ let slug = baseSlug;
30
+ let counter = 1;
31
+ while (existingSlugs.has(slug)) {
32
+ slug = `${baseSlug}-${counter}`;
33
+ counter++;
34
+ }
35
+ existingSlugs.add(slug);
36
+ return slug;
37
+ }
@@ -0,0 +1,35 @@
1
+ import type { ADFNode } from "../types";
2
+ import { ConfluenceClient } from "../client/confluence-client";
3
+ /**
4
+ * Link enrichment result containing metadata about a Confluence page link
5
+ */
6
+ export interface EnrichedLinkData {
7
+ pageId: string;
8
+ title: string;
9
+ originalUrl: string;
10
+ }
11
+ /**
12
+ * Options for link processing
13
+ */
14
+ export interface LinkProcessorOptions {
15
+ confluenceClient: ConfluenceClient;
16
+ confluenceBaseUrl: string;
17
+ }
18
+ /**
19
+ * Process ADF to enrich Confluence page links with page titles
20
+ *
21
+ * This processor:
22
+ * 1. Recursively traverses the ADF tree
23
+ * 2. Finds all links (marks and inlineCard nodes) pointing to Confluence pages
24
+ * 3. Extracts page IDs from URLs
25
+ * 4. Fetches page titles from Confluence API
26
+ * 5. Enriches link nodes with page metadata
27
+ *
28
+ * @param adf - The ADF document to process
29
+ * @param options - Link processor options (client and base URL)
30
+ * @returns Processed ADF with enriched link data and map of enriched links
31
+ */
32
+ export declare function processADFWithLinks(adf: ADFNode, options: LinkProcessorOptions): Promise<{
33
+ adf: ADFNode;
34
+ enrichedLinks: Map<string, EnrichedLinkData>;
35
+ }>;
@@ -0,0 +1,174 @@
1
+ import { ConfluenceClient } from "../client/confluence-client";
2
+ /**
3
+ * Process ADF to enrich Confluence page links with page titles
4
+ *
5
+ * This processor:
6
+ * 1. Recursively traverses the ADF tree
7
+ * 2. Finds all links (marks and inlineCard nodes) pointing to Confluence pages
8
+ * 3. Extracts page IDs from URLs
9
+ * 4. Fetches page titles from Confluence API
10
+ * 5. Enriches link nodes with page metadata
11
+ *
12
+ * @param adf - The ADF document to process
13
+ * @param options - Link processor options (client and base URL)
14
+ * @returns Processed ADF with enriched link data and map of enriched links
15
+ */
16
+ export async function processADFWithLinks(adf, options) {
17
+ const enrichedLinks = new Map();
18
+ const { confluenceClient, confluenceBaseUrl } = options;
19
+ // Extract all Confluence page URLs from the ADF
20
+ const confluenceUrls = extractConfluenceUrls(adf, confluenceBaseUrl);
21
+ // Fetch page titles for all unique page IDs
22
+ const pageTitles = await fetchPageTitles(confluenceUrls, confluenceClient);
23
+ // Store enriched link data
24
+ pageTitles.forEach((data) => {
25
+ enrichedLinks.set(data.originalUrl, data);
26
+ });
27
+ // Clone and enrich the ADF tree
28
+ const enrichedAdf = enrichAdfWithLinkData(adf, enrichedLinks);
29
+ return { adf: enrichedAdf, enrichedLinks };
30
+ }
31
+ /**
32
+ * Extract all Confluence page URLs from the ADF tree
33
+ *
34
+ * @param node - Current ADF node to process
35
+ * @param confluenceBaseUrl - Base URL to match against
36
+ * @param urls - Set to accumulate found URLs
37
+ * @returns Set of unique Confluence page URLs
38
+ */
39
+ function extractConfluenceUrls(node, confluenceBaseUrl, urls = new Set()) {
40
+ // Check for inline card nodes
41
+ if (node.type === "inlineCard" && node.attrs?.url) {
42
+ const url = node.attrs.url;
43
+ if (isConfluenceUrl(url, confluenceBaseUrl)) {
44
+ urls.add(url);
45
+ }
46
+ }
47
+ // Check for link marks in text nodes
48
+ if (node.marks && Array.isArray(node.marks)) {
49
+ for (const mark of node.marks) {
50
+ if (mark.type === "link" && mark.attrs?.href) {
51
+ const href = mark.attrs.href;
52
+ if (isConfluenceUrl(href, confluenceBaseUrl)) {
53
+ urls.add(href);
54
+ }
55
+ }
56
+ }
57
+ }
58
+ // Recursively process child nodes
59
+ if (node.content && Array.isArray(node.content)) {
60
+ for (const child of node.content) {
61
+ extractConfluenceUrls(child, confluenceBaseUrl, urls);
62
+ }
63
+ }
64
+ return urls;
65
+ }
66
+ /**
67
+ * Check if a URL points to a Confluence page
68
+ *
69
+ * @param url - URL to check
70
+ * @param confluenceBaseUrl - Base URL to match against
71
+ * @returns true if URL is a Confluence page URL
72
+ */
73
+ function isConfluenceUrl(url, confluenceBaseUrl) {
74
+ try {
75
+ // Normalize base URL for comparison (remove trailing slash)
76
+ const normalizedBase = confluenceBaseUrl.replace(/\/$/, "");
77
+ // Check if URL starts with the base URL
78
+ if (!url.startsWith(normalizedBase)) {
79
+ return false;
80
+ }
81
+ // Check if we can extract a page ID
82
+ const pageId = ConfluenceClient.extractPageIdFromUrl(url);
83
+ return pageId !== null;
84
+ }
85
+ catch {
86
+ return false;
87
+ }
88
+ }
89
+ /**
90
+ * Fetch page titles for a set of Confluence URLs
91
+ *
92
+ * @param urls - Set of Confluence page URLs
93
+ * @param client - Confluence API client
94
+ * @returns Array of enriched link data
95
+ */
96
+ async function fetchPageTitles(urls, client) {
97
+ const results = [];
98
+ // Group URLs by page ID to avoid duplicate API calls
99
+ const pageIdMap = new Map();
100
+ for (const url of urls) {
101
+ const pageId = ConfluenceClient.extractPageIdFromUrl(url);
102
+ if (pageId) {
103
+ if (!pageIdMap.has(pageId)) {
104
+ pageIdMap.set(pageId, []);
105
+ }
106
+ pageIdMap.get(pageId).push(url);
107
+ }
108
+ }
109
+ // Fetch page data for each unique page ID
110
+ await Promise.all(Array.from(pageIdMap.entries()).map(async ([pageId, pageUrls]) => {
111
+ try {
112
+ const page = await client.getPage(pageId, false);
113
+ // Add enriched data for all URLs pointing to this page
114
+ for (const url of pageUrls) {
115
+ results.push({
116
+ pageId,
117
+ title: page.title,
118
+ originalUrl: url,
119
+ });
120
+ }
121
+ }
122
+ catch (error) {
123
+ // If we can't fetch the page, skip enrichment for these URLs
124
+ console.warn(`Failed to fetch page ${pageId}:`, error);
125
+ }
126
+ }));
127
+ return results;
128
+ }
129
+ /**
130
+ * Recursively enrich ADF tree with link metadata
131
+ *
132
+ * @param node - Current ADF node
133
+ * @param enrichedLinks - Map of enriched link data
134
+ * @returns Cloned and enriched ADF node
135
+ */
136
+ function enrichAdfWithLinkData(node, enrichedLinks) {
137
+ // Clone the node to avoid mutations
138
+ const clonedNode = { ...node };
139
+ // Enrich inline card nodes
140
+ if (clonedNode.type === "inlineCard" && clonedNode.attrs?.url) {
141
+ const enrichedData = enrichedLinks.get(clonedNode.attrs.url);
142
+ if (enrichedData) {
143
+ clonedNode.attrs = {
144
+ ...clonedNode.attrs,
145
+ title: enrichedData.title,
146
+ pageId: enrichedData.pageId,
147
+ };
148
+ }
149
+ }
150
+ // Enrich link marks
151
+ if (clonedNode.marks && Array.isArray(clonedNode.marks)) {
152
+ clonedNode.marks = clonedNode.marks.map((mark) => {
153
+ if (mark.type === "link" && mark.attrs?.href) {
154
+ const enrichedData = enrichedLinks.get(mark.attrs.href);
155
+ if (enrichedData) {
156
+ return {
157
+ ...mark,
158
+ attrs: {
159
+ ...mark.attrs,
160
+ title: enrichedData.title,
161
+ pageId: enrichedData.pageId,
162
+ },
163
+ };
164
+ }
165
+ }
166
+ return mark;
167
+ });
168
+ }
169
+ // Recursively process child nodes
170
+ if (clonedNode.content && Array.isArray(clonedNode.content)) {
171
+ clonedNode.content = clonedNode.content.map((child) => enrichAdfWithLinkData(child, enrichedLinks));
172
+ }
173
+ return clonedNode;
174
+ }
@@ -0,0 +1,2 @@
1
+ import { ADFDocument } from "../types";
2
+ export declare function processADFWithMedia(adfDocument: ADFDocument, storageHtml: string, pageId: string): Promise<ADFDocument>;
@@ -0,0 +1,160 @@
1
+ // Helper function to determine if a filename is an image or video
2
+ function isImageFile(filename) {
3
+ const imageExtensions = [
4
+ ".jpg",
5
+ ".jpeg",
6
+ ".png",
7
+ ".gif",
8
+ ".webp",
9
+ ".bmp",
10
+ ".svg",
11
+ ];
12
+ return imageExtensions.some((ext) => filename.toLowerCase().endsWith(ext));
13
+ }
14
+ function isVideoFile(filename) {
15
+ const videoExtensions = [
16
+ ".mp4",
17
+ ".webm",
18
+ ".ogg",
19
+ ".mov",
20
+ ".avi",
21
+ ".mkv",
22
+ ".flv",
23
+ ];
24
+ return videoExtensions.some((ext) => filename.toLowerCase().endsWith(ext));
25
+ }
26
+ // Extract media mappings from Storage HTML (server-side version)
27
+ function extractMediaMappings(storageHtml, pageId) {
28
+ const mediaMap = {};
29
+ const baseUrl = process.env.CONFLUENCE_BASE_URL;
30
+ if (!baseUrl) {
31
+ console.error("CONFLUENCE_BASE_URL not configured");
32
+ return mediaMap;
33
+ }
34
+ // Pattern for extracting ALL attachments from Storage format
35
+ const attachmentRegex = /<ri:attachment ri:filename="([^"]*)"[^>]*\/?>/gi;
36
+ let match;
37
+ let imageIndex = 0;
38
+ let videoIndex = 0;
39
+ let otherIndex = 0;
40
+ const processedFiles = new Set();
41
+ // Extract all attachments and categorize them by file extension
42
+ while ((match = attachmentRegex.exec(storageHtml)) !== null) {
43
+ const filename = match[1];
44
+ if (processedFiles.has(filename))
45
+ continue;
46
+ processedFiles.add(filename);
47
+ const fileUrl = `${baseUrl}/wiki/download/attachments/${pageId}/${encodeURIComponent(filename)}`;
48
+ // Create mapping with type information
49
+ let mediaType = "unknown";
50
+ let indexKey;
51
+ if (isImageFile(filename)) {
52
+ mediaType = "image";
53
+ indexKey = `image-${imageIndex}`;
54
+ imageIndex++;
55
+ }
56
+ else if (isVideoFile(filename)) {
57
+ mediaType = "video";
58
+ indexKey = `video-${videoIndex}`;
59
+ videoIndex++;
60
+ }
61
+ else {
62
+ mediaType = "file";
63
+ indexKey = `attachment-${otherIndex}`;
64
+ otherIndex++;
65
+ }
66
+ const mediaMapping = {
67
+ url: fileUrl,
68
+ type: mediaType,
69
+ fileName: filename,
70
+ };
71
+ // Store with multiple keys for easy lookup
72
+ mediaMap[indexKey] = mediaMapping;
73
+ mediaMap[filename] = mediaMapping;
74
+ // console.log(`Mapped ${mediaType} ${indexKey}: ${filename} -> ${fileUrl}`);
75
+ }
76
+ return mediaMap;
77
+ }
78
+ // Process ADF document to enrich media nodes with URLs and types
79
+ function enrichMediaNodes(node, mediaMappings, mediaCounter) {
80
+ if (!node)
81
+ return node;
82
+ // Handle media nodes
83
+ if (node.type === "media" && node.attrs?.type === "file" && node.attrs?.id) {
84
+ const mediaId = node.attrs.id;
85
+ // Special handling for showroom page demo image
86
+ if (mediaId === "placeholder-image") {
87
+ const enrichedNode = {
88
+ ...node,
89
+ attrs: {
90
+ ...node.attrs,
91
+ // Add our processed data with Unsplash demo image
92
+ processedUrl: "https://images.unsplash.com/photo-1498050108023-c5249f4df085?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1200&q=80",
93
+ processedType: "image",
94
+ },
95
+ };
96
+ mediaCounter.count++;
97
+ return enrichedNode;
98
+ }
99
+ // Try to find media mapping using various strategies
100
+ const possibleKeys = [
101
+ mediaId,
102
+ // Try sequential mapping based on media element order
103
+ `image-${mediaCounter.count}`,
104
+ `video-${mediaCounter.count}`,
105
+ `attachment-${mediaCounter.count}`,
106
+ // Try filename matches
107
+ ...Object.keys(mediaMappings).filter((key) => !key.startsWith("image-") &&
108
+ !key.startsWith("video-") &&
109
+ !key.startsWith("attachment-")),
110
+ // Try all indexed keys as fallback
111
+ ...Object.keys(mediaMappings)
112
+ .filter((key) => key.startsWith("image-") ||
113
+ key.startsWith("video-") ||
114
+ key.startsWith("attachment-"))
115
+ .sort(),
116
+ ];
117
+ let foundMapping = null;
118
+ for (const key of possibleKeys) {
119
+ if (mediaMappings[key]) {
120
+ foundMapping = mediaMappings[key];
121
+ // console.log(`Media element ${mediaCounter.count}: Found mapping for ${mediaId} via key "${key}"`);
122
+ break;
123
+ }
124
+ }
125
+ // Enrich the node with URL and type
126
+ if (foundMapping) {
127
+ const enrichedNode = {
128
+ ...node,
129
+ attrs: {
130
+ ...node.attrs,
131
+ // Add our processed data
132
+ processedUrl: foundMapping.url,
133
+ processedType: foundMapping.type,
134
+ processedFileName: foundMapping.fileName,
135
+ },
136
+ };
137
+ mediaCounter.count++;
138
+ return enrichedNode;
139
+ }
140
+ mediaCounter.count++;
141
+ }
142
+ // Recursively process content
143
+ if (node.content) {
144
+ return {
145
+ ...node,
146
+ content: node.content.map((child) => enrichMediaNodes(child, mediaMappings, mediaCounter)),
147
+ };
148
+ }
149
+ return node;
150
+ }
151
+ // Main function to process ADF document with media enrichment
152
+ export async function processADFWithMedia(adfDocument, storageHtml, pageId) {
153
+ // Extract media mappings from Storage HTML
154
+ const mediaMappings = extractMediaMappings(storageHtml, pageId);
155
+ // Enrich ADF document with media URLs
156
+ const mediaCounter = { count: 0 };
157
+ const enrichedDocument = enrichMediaNodes(adfDocument, mediaMappings, mediaCounter);
158
+ // console.log(`Processed ${mediaCounter.count} media elements for page ${pageId}`);
159
+ return enrichedDocument;
160
+ }
@@ -0,0 +1,5 @@
1
+ import { ADFDocument, TocItem } from "../types";
2
+ export declare function processADFWithTOC(adfDocument: ADFDocument): {
3
+ enrichedDocument: ADFDocument;
4
+ tableOfContents: TocItem[];
5
+ };
@@ -0,0 +1,90 @@
1
+ // Extract text content from ADF nodes
2
+ function extractTextFromContent(content) {
3
+ return content
4
+ .map((node) => {
5
+ if (node.type === "text" && node.text) {
6
+ return node.text;
7
+ }
8
+ if (node.content) {
9
+ return extractTextFromContent(node.content);
10
+ }
11
+ return "";
12
+ })
13
+ .join("");
14
+ }
15
+ // Generate unique slug for heading
16
+ function generateSlug(text, existingSlugs) {
17
+ let baseSlug = text
18
+ .toLowerCase()
19
+ .replace(/[^a-z0-9\s-]/g, "")
20
+ .replace(/\s+/g, "-")
21
+ .trim();
22
+ let slug = baseSlug;
23
+ let counter = 1;
24
+ while (existingSlugs.has(slug)) {
25
+ slug = `${baseSlug}-${counter}`;
26
+ counter++;
27
+ }
28
+ existingSlugs.add(slug);
29
+ return slug;
30
+ }
31
+ // Extract headings from ADF document (server-side)
32
+ function extractHeadings(node, headings, existingSlugs) {
33
+ if (!node)
34
+ return;
35
+ if (node.type === "heading" && node.content) {
36
+ const level = node.attrs?.level || 1;
37
+ const title = extractTextFromContent(node.content);
38
+ if (title) {
39
+ const id = generateSlug(title, existingSlugs);
40
+ headings.push({ id, title, level });
41
+ // console.log('Extracted heading for TOC:', { id, title, level });
42
+ }
43
+ }
44
+ if (node.content) {
45
+ node.content.forEach((child) => extractHeadings(child, headings, existingSlugs));
46
+ }
47
+ }
48
+ // Process ADF document to enrich heading nodes with IDs and extract TOC
49
+ function enrichHeadingNodes(node, existingSlugs) {
50
+ if (!node)
51
+ return node;
52
+ // Handle heading nodes
53
+ if (node.type === "heading" && node.content) {
54
+ const title = extractTextFromContent(node.content);
55
+ if (title) {
56
+ const id = generateSlug(title, existingSlugs);
57
+ return {
58
+ ...node,
59
+ attrs: {
60
+ ...node.attrs,
61
+ // Add generated ID for anchor links
62
+ generatedId: id,
63
+ },
64
+ };
65
+ }
66
+ }
67
+ // Recursively process content
68
+ if (node.content) {
69
+ return {
70
+ ...node,
71
+ content: node.content.map((child) => enrichHeadingNodes(child, existingSlugs)),
72
+ };
73
+ }
74
+ return node;
75
+ }
76
+ // Main function to process ADF document with TOC extraction
77
+ export function processADFWithTOC(adfDocument) {
78
+ const headings = [];
79
+ const existingSlugs = new Set();
80
+ // First pass: extract headings for TOC
81
+ extractHeadings(adfDocument, headings, existingSlugs);
82
+ // Second pass: enrich heading nodes with IDs (reuse the same slugs)
83
+ existingSlugs.clear(); // Reset for consistent ID generation
84
+ const enrichedDocument = enrichHeadingNodes(adfDocument, existingSlugs);
85
+ // console.log(`Extracted ${headings.length} headings for TOC`);
86
+ return {
87
+ enrichedDocument,
88
+ tableOfContents: headings,
89
+ };
90
+ }