@gravitek/confluence-mirror-core 0.2.0 → 0.3.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/LICENSE +200 -0
- package/NOTICE +9 -0
- package/README.md +94 -9
- package/dist/client/confluence-client.d.ts +53 -5
- package/dist/client/confluence-client.js +191 -24
- package/dist/client/content-types.d.ts +55 -0
- package/dist/client/content-types.js +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/processors/link-processor.d.ts +11 -5
- package/dist/processors/link-processor.js +33 -29
- package/dist/utils/url-parser.d.ts +38 -0
- package/dist/utils/url-parser.js +63 -0
- package/dist/utils/url-transformer.d.ts +5 -5
- package/dist/utils/url-transformer.js +12 -12
- package/package.json +12 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity granting the License.
|
|
13
|
+
|
|
14
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
15
|
+
other entities that control, are controlled by, or are under common
|
|
16
|
+
control with that entity. For the purposes of this definition,
|
|
17
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
18
|
+
direction or management of such entity, whether by contract or
|
|
19
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
23
|
+
exercising permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
26
|
+
including but not limited to software source code, documentation
|
|
27
|
+
source, and configuration files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical
|
|
30
|
+
transformation or translation of a Source form, including but
|
|
31
|
+
not limited to compiled object code, generated documentation,
|
|
32
|
+
and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
35
|
+
Object form, made available under the License, as indicated by a
|
|
36
|
+
copyright notice that is included in or attached to the work
|
|
37
|
+
(which shall not include material not subject to the License).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based upon (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and derivative works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including
|
|
48
|
+
the original version of the Work and any modifications or additions
|
|
49
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
50
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
51
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
52
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
53
|
+
means any form of electronic, verbal, or written communication sent
|
|
54
|
+
to the Licensor or its representatives, including but not limited to
|
|
55
|
+
communication on electronic mailing lists, source code control
|
|
56
|
+
systems, and issue tracking systems that are managed by, or on behalf
|
|
57
|
+
of, the Licensor for the purpose of discussing and improving the Work,
|
|
58
|
+
but excluding communication that is conspicuously marked or otherwise
|
|
59
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
60
|
+
|
|
61
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
62
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
63
|
+
subsequently incorporated within the Work.
|
|
64
|
+
|
|
65
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
66
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
67
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
68
|
+
copyright license to use, reproduce, modify, publicly display,
|
|
69
|
+
publicly perform, sublicense, and distribute the Work and such
|
|
70
|
+
Derivative Works in Source or Object form.
|
|
71
|
+
|
|
72
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
73
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
74
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
75
|
+
(except as stated in this section) patent license to make, have made,
|
|
76
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
77
|
+
where such license applies only to those patent claims licensable
|
|
78
|
+
by such Contributor that are necessarily infringed by their
|
|
79
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
80
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
81
|
+
institute patent litigation against any entity (including a
|
|
82
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
83
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
84
|
+
or contributory patent infringement, then any patent licenses
|
|
85
|
+
granted to You under this License for that Work shall terminate
|
|
86
|
+
as of the date such litigation is filed.
|
|
87
|
+
|
|
88
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
89
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
90
|
+
modifications, and in Source or Object form, provided that You
|
|
91
|
+
meet the following conditions:
|
|
92
|
+
|
|
93
|
+
(a) You must give any other recipients of the Work or
|
|
94
|
+
Derivative Works a copy of this License; and
|
|
95
|
+
|
|
96
|
+
(b) You must cause any modified files to carry prominent notices
|
|
97
|
+
stating that You changed the files; and
|
|
98
|
+
|
|
99
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
100
|
+
that You distribute, all copyright, patent, trademark, and
|
|
101
|
+
attribution notices from the Source form of the Work,
|
|
102
|
+
excluding those notices that do not pertain to any part of
|
|
103
|
+
the Derivative Works; and
|
|
104
|
+
|
|
105
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
106
|
+
distribution, then any Derivative Works that You distribute must
|
|
107
|
+
include a readable copy of the attribution notices contained
|
|
108
|
+
within such NOTICE file, excluding those notices that do not
|
|
109
|
+
pertain to any part of the Derivative Works, in at least one
|
|
110
|
+
of the following places: within a NOTICE text file distributed
|
|
111
|
+
as part of the Derivative Works; within the Source form or
|
|
112
|
+
documentation, if provided along with the Derivative Works; or,
|
|
113
|
+
within a display generated by the Derivative Works, if and
|
|
114
|
+
wherever such third-party notices normally appear. The contents
|
|
115
|
+
of the NOTICE file are for informational purposes only and
|
|
116
|
+
do not modify the License. You may add Your own attribution
|
|
117
|
+
notices within Derivative Works that You distribute, alongside
|
|
118
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
119
|
+
that such additional attribution notices cannot be construed
|
|
120
|
+
as modifying the License.
|
|
121
|
+
|
|
122
|
+
You may add Your own copyright notice to Your modifications and
|
|
123
|
+
may provide additional or different license terms and conditions
|
|
124
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
125
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
126
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
127
|
+
the conditions stated in this License.
|
|
128
|
+
|
|
129
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
130
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
131
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
132
|
+
this License, without any additional terms or conditions.
|
|
133
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
134
|
+
the terms of any separate license agreement you may have executed
|
|
135
|
+
with Licensor regarding such Contributions.
|
|
136
|
+
|
|
137
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
138
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
139
|
+
except as required for reasonable and customary use in describing the
|
|
140
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
141
|
+
|
|
142
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
143
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
144
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
145
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
146
|
+
implied, including, without limitation, any warranties or conditions
|
|
147
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
148
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
149
|
+
appropriateness of using or redistributing the Work and assume any
|
|
150
|
+
risks associated with Your exercise of permissions under this License.
|
|
151
|
+
|
|
152
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
153
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
154
|
+
unless required by applicable law (such as deliberate and grossly
|
|
155
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
156
|
+
liable to You for damages, including any direct, indirect, special,
|
|
157
|
+
incidental, or consequential damages of any character arising as a
|
|
158
|
+
result of this License or out of the use or inability to use the
|
|
159
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
160
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
161
|
+
other commercial damages or losses), even if such Contributor
|
|
162
|
+
has been advised of the possibility of such damages.
|
|
163
|
+
|
|
164
|
+
9. Accepting Warranty or Additional Support. When redistributing
|
|
165
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
166
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
167
|
+
or other liability obligations and/or rights consistent with this
|
|
168
|
+
License. However, in accepting such obligations, You may act only
|
|
169
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
170
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
171
|
+
defend, and hold each Contributor harmless for any liability
|
|
172
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
173
|
+
of your accepting any such warranty or additional support.
|
|
174
|
+
|
|
175
|
+
END OF TERMS AND CONDITIONS
|
|
176
|
+
|
|
177
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
178
|
+
|
|
179
|
+
To apply the Apache License to your work, attach the following
|
|
180
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
181
|
+
replaced with your own identifying information. Don't include
|
|
182
|
+
the brackets! The text should be enclosed in the appropriate
|
|
183
|
+
comment syntax for the file format. We also recommend that a
|
|
184
|
+
file or class name and description of purpose be included on the
|
|
185
|
+
same "printed page" as the copyright notice for easier
|
|
186
|
+
identification within third-party archives.
|
|
187
|
+
|
|
188
|
+
Copyright 2025 Gravitek
|
|
189
|
+
|
|
190
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
191
|
+
you may not use this file except in compliance with the License.
|
|
192
|
+
You may obtain a copy of the License at
|
|
193
|
+
|
|
194
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
195
|
+
|
|
196
|
+
Unless required by applicable law or agreed to in writing, software
|
|
197
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
198
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
199
|
+
See the License for the specific language governing permissions and
|
|
200
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
@gravitek/confluence-mirror-core
|
|
2
|
+
Copyright 2025-2026 Gravitek
|
|
3
|
+
|
|
4
|
+
This product includes software developed at Gravitek.
|
|
5
|
+
|
|
6
|
+
This is an unofficial library, not affiliated with or endorsed by Atlassian.
|
|
7
|
+
Confluence and Atlassian are trademarks of Atlassian Pty Ltd.
|
|
8
|
+
|
|
9
|
+
This package has no runtime dependencies and contains no third-party code.
|
package/README.md
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
# confluence-mirror-core
|
|
1
|
+
# @gravitek/confluence-mirror-core
|
|
2
2
|
|
|
3
3
|
Core logic for Confluence page rendering - framework agnostic.
|
|
4
4
|
|
|
5
|
+
Requires Node.js 22 or later.
|
|
6
|
+
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
|
-
npm install confluence-mirror-core
|
|
10
|
+
npm install @gravitek/confluence-mirror-core
|
|
9
11
|
```
|
|
10
12
|
|
|
11
13
|
## Features
|
|
12
14
|
|
|
13
15
|
- ✅ **Confluence REST API Client** - Authenticate and fetch pages
|
|
16
|
+
- ✅ **Folder Browsing** - Read folders, direct children, ancestors and content summaries (REST v2)
|
|
14
17
|
- ✅ **ADF Processing** - Parse Atlas Document Format
|
|
15
18
|
- ✅ **Media Processing** - Extract and rewrite media URLs
|
|
16
19
|
- ✅ **TOC Extraction** - Generate table of contents from headings
|
|
@@ -24,12 +27,12 @@ import {
|
|
|
24
27
|
ConfluenceClient,
|
|
25
28
|
processADFWithMedia,
|
|
26
29
|
processADFWithTOC
|
|
27
|
-
} from 'confluence-mirror-core';
|
|
30
|
+
} from '@gravitek/confluence-mirror-core';
|
|
28
31
|
|
|
29
32
|
// Initialize client
|
|
30
|
-
const client =
|
|
33
|
+
const client = ConfluenceClient.createWithBasicAuth(
|
|
31
34
|
"https://your-domain.atlassian.net",
|
|
32
|
-
"your-email@domain.com",
|
|
35
|
+
"your-email@domain.com",
|
|
33
36
|
"your-api-key"
|
|
34
37
|
);
|
|
35
38
|
|
|
@@ -53,16 +56,98 @@ const { enrichedDocument, tableOfContents } = processADFWithTOC(processedContent
|
|
|
53
56
|
|
|
54
57
|
```typescript
|
|
55
58
|
class ConfluenceClient {
|
|
56
|
-
constructor(
|
|
57
|
-
|
|
59
|
+
constructor(authStrategy: AuthStrategy, baseUrl?: string)
|
|
60
|
+
static createWithBasicAuth(baseUrl: string, email: string, apiKey: string): ConfluenceClient
|
|
61
|
+
static createWithOAuth2(clientId: string, clientSecret: string): ConfluenceClient
|
|
62
|
+
|
|
63
|
+
|
|
58
64
|
async getPage(pageId: string, includeChildren?: boolean): Promise<ConfluencePage>
|
|
59
65
|
async getChildPages(pageId: string): Promise<ConfluenceChildPage[]>
|
|
60
66
|
async getCurrentUser(): Promise<any>
|
|
61
|
-
|
|
67
|
+
|
|
68
|
+
// Folders and hierarchy (REST v2, since 0.3.0)
|
|
69
|
+
async getFolder(id: string): Promise<ConfluenceFolder>
|
|
70
|
+
async getDirectChildren(
|
|
71
|
+
parentType: ConfluenceContentType,
|
|
72
|
+
id: string,
|
|
73
|
+
options?: { maxItems?: number }
|
|
74
|
+
): Promise<DirectChildrenResult>
|
|
75
|
+
async getAncestors(type: ConfluenceContentType, id: string): Promise<ConfluenceAncestor[]>
|
|
76
|
+
async getContentSummary(type: ConfluenceContentType, id: string): Promise<ConfluenceContentSummary>
|
|
77
|
+
|
|
62
78
|
static extractPageIdFromUrl(url: string): string | null
|
|
63
79
|
}
|
|
64
80
|
```
|
|
65
81
|
|
|
82
|
+
### Folders and hierarchy
|
|
83
|
+
|
|
84
|
+
Since 0.3.0, the client reads folders and the page tree through the Confluence REST v2 API. In Basic Auth mode these methods use REST v2 too (`/wiki/api/v2`).
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
// A folder, with its space ({ key, name }, or null if the space lookup failed)
|
|
88
|
+
const folder = await client.getFolder("1001");
|
|
89
|
+
console.log(folder.title, folder.space?.key);
|
|
90
|
+
|
|
91
|
+
// Direct children of a page or folder, in tree order (default cap: 1000 items)
|
|
92
|
+
const { items, truncated } = await client.getDirectChildren("folder", "1001", {
|
|
93
|
+
maxItems: 200,
|
|
94
|
+
});
|
|
95
|
+
for (const item of items) console.log(item.type, item.id, item.title);
|
|
96
|
+
|
|
97
|
+
// Ancestors, root first
|
|
98
|
+
const ancestors = await client.getAncestors("page", "2002");
|
|
99
|
+
|
|
100
|
+
// Metadata without the body (title, spaceId, parentId, parentType)
|
|
101
|
+
const summary = await client.getContentSummary("folder", "1001");
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The content types accepted by these reads are `page`, `folder`, `whiteboard`, `database` and `embed`. Only `page` and `folder` have been verified against a live Confluence site; any other type is rejected with a `ConfluenceApiError` before a request is sent. Content IDs must be numeric (`/^\d+$/`): any other value is rejected the same way, for every client method that takes an ID.
|
|
105
|
+
|
|
106
|
+
### Folder URLs
|
|
107
|
+
|
|
108
|
+
URL helpers recognise folder URLs such as `https://example.atlassian.net/wiki/spaces/DOC/folder/1001/Guides`:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import {
|
|
112
|
+
extractFolderIdFromUrl,
|
|
113
|
+
extractContentFromUrl,
|
|
114
|
+
UrlTransformer,
|
|
115
|
+
} from '@gravitek/confluence-mirror-core';
|
|
116
|
+
|
|
117
|
+
extractFolderIdFromUrl("https://example.atlassian.net/wiki/spaces/DOC/folder/1001/Guides"); // "1001"
|
|
118
|
+
extractContentFromUrl("https://example.atlassian.net/wiki/spaces/DOC/pages/2002/Intro"); // { type: "page", id: "2002" }
|
|
119
|
+
|
|
120
|
+
// Folder links are routed to `?folderId=`, page links to `?pageId=`
|
|
121
|
+
const transformer = new UrlTransformer({
|
|
122
|
+
confluenceBaseUrl: "https://example.atlassian.net",
|
|
123
|
+
localBaseUrl: "https://docs.example.com",
|
|
124
|
+
});
|
|
125
|
+
transformer.toLocalUrl("https://example.atlassian.net/wiki/spaces/DOC/folder/1001/Guides");
|
|
126
|
+
// "https://docs.example.com/?folderId=1001"
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`processADFWithLinks()` also enriches folder links with the folder title. `EnrichedLinkData` gains a `contentType` field (`"page" | "folder"`); `pageId` holds the folder ID for folders.
|
|
130
|
+
|
|
131
|
+
### Required OAuth2 scopes
|
|
132
|
+
|
|
133
|
+
A service account (OAuth2) needs these granular scopes, all suffixed with `:confluence`:
|
|
134
|
+
|
|
135
|
+
| Scope | Used for |
|
|
136
|
+
| --- | --- |
|
|
137
|
+
| `read:page:confluence` | Reading pages and their child pages |
|
|
138
|
+
| `read:attachment:confluence` | Attachments and media of a page |
|
|
139
|
+
| `read:user:confluence` | Page authors and the current user |
|
|
140
|
+
| `read:label:confluence` | Page labels |
|
|
141
|
+
| `read:space:confluence` | Resolving the space (key and name) of a page or folder |
|
|
142
|
+
| `read:embed:confluence` | Embedded content |
|
|
143
|
+
| `read:folder:confluence` | `getFolder`, and `getContentSummary("folder", …)` (needed since 0.3.0) |
|
|
144
|
+
| `read:hierarchical-content:confluence` | `getDirectChildren` for pages and folders (needed since 0.3.0) |
|
|
145
|
+
| `read:content.metadata:confluence` | `getAncestors` for pages and folders (needed since 0.3.0) |
|
|
146
|
+
|
|
147
|
+
Scopes per method, as listed in Atlassian's REST v2 OpenAPI specification: `getFolder` needs `read:folder` and `read:space`; `getDirectChildren` needs `read:hierarchical-content`; `getAncestors` needs `read:content.metadata`; `getContentSummary` needs `read:page` (page) or `read:folder` (folder).
|
|
148
|
+
|
|
149
|
+
The service account must also have access to the Confluence spaces it reads.
|
|
150
|
+
|
|
66
151
|
### Processing Functions
|
|
67
152
|
|
|
68
153
|
```typescript
|
|
@@ -116,7 +201,7 @@ interface TocItem {
|
|
|
116
201
|
## Error Handling
|
|
117
202
|
|
|
118
203
|
```typescript
|
|
119
|
-
import { ConfluenceApiError } from 'confluence-mirror-core';
|
|
204
|
+
import { ConfluenceApiError } from '@gravitek/confluence-mirror-core';
|
|
120
205
|
|
|
121
206
|
try {
|
|
122
207
|
const page = await client.getPage("invalid-id");
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { AuthStrategy } from "./auth-strategies";
|
|
2
|
+
import { type ConfluenceContentRef } from "../utils/url-parser";
|
|
3
|
+
import type { ConfluenceAncestor, ConfluenceContentSummary, ConfluenceContentType, ConfluenceFolder, DirectChildrenResult } from "./content-types";
|
|
2
4
|
export interface ConfluencePage {
|
|
3
5
|
id: string;
|
|
4
6
|
title: string;
|
|
@@ -79,20 +81,66 @@ export declare class ConfluenceClient {
|
|
|
79
81
|
* - OAuth2: https://api.atlassian.com/ex/confluence/{cloudId}/wiki/api/v2/{endpoint} (API v2)
|
|
80
82
|
*/
|
|
81
83
|
private getApiUrl;
|
|
84
|
+
/**
|
|
85
|
+
* Construct a REST v2 URL whatever the authentication strategy:
|
|
86
|
+
* - Basic Auth: {baseUrl}/wiki/api/v2/{endpoint}
|
|
87
|
+
* - OAuth2: https://api.atlassian.com/ex/confluence/{cloudId}/wiki/api/v2/{endpoint}
|
|
88
|
+
*/
|
|
89
|
+
private getApiUrlV2;
|
|
82
90
|
/**
|
|
83
91
|
* Make an authenticated request to the Confluence API
|
|
84
92
|
* Handles token refresh on 401 errors for OAuth2
|
|
93
|
+
* @param options.v2 - Force REST v2 (default: v1 in Basic Auth, v2 in OAuth2)
|
|
94
|
+
* @param options.retryCount - Internal: number of retries already done
|
|
85
95
|
*/
|
|
86
96
|
private makeRequest;
|
|
87
97
|
getPage(pageId: string, includeChildren?: boolean): Promise<ConfluencePage>;
|
|
88
98
|
getChildPages(pageId: string): Promise<ConfluenceChildPage[]>;
|
|
99
|
+
/**
|
|
100
|
+
* Map a content type to its REST v2 endpoint segment.
|
|
101
|
+
* Throws without any network call for unsupported types.
|
|
102
|
+
*/
|
|
103
|
+
private v2Segment;
|
|
104
|
+
/**
|
|
105
|
+
* Ensure an ID is purely numeric before it is interpolated into a URL path.
|
|
106
|
+
* Without this, `..` segments would be resolved by the URL parser and could
|
|
107
|
+
* reach other endpoints on the credentialed host. No request is sent.
|
|
108
|
+
*/
|
|
109
|
+
private assertNumericId;
|
|
110
|
+
/**
|
|
111
|
+
* Read a folder (REST v2). The space is resolved with a second call;
|
|
112
|
+
* `space` is `null` when that lookup fails, never the space ID.
|
|
113
|
+
*/
|
|
114
|
+
getFolder(id: string): Promise<ConfluenceFolder>;
|
|
115
|
+
/**
|
|
116
|
+
* List the direct children of a page or folder (REST v2), following
|
|
117
|
+
* cursor pagination. Stops once `maxItems` (default 1000) items are
|
|
118
|
+
* collected and reports it with `truncated`.
|
|
119
|
+
*/
|
|
120
|
+
getDirectChildren(parentType: ConfluenceContentType, id: string, options?: {
|
|
121
|
+
maxItems?: number;
|
|
122
|
+
}): Promise<DirectChildrenResult>;
|
|
123
|
+
/**
|
|
124
|
+
* List the ancestors of a content item (REST v2), root first as returned.
|
|
125
|
+
*/
|
|
126
|
+
getAncestors(type: ConfluenceContentType, id: string): Promise<ConfluenceAncestor[]>;
|
|
127
|
+
/**
|
|
128
|
+
* Read the metadata of a content item without its body (REST v2).
|
|
129
|
+
* `type` is the one asked for, not read from the response body.
|
|
130
|
+
*/
|
|
131
|
+
getContentSummary(type: ConfluenceContentType, id: string): Promise<ConfluenceContentSummary>;
|
|
89
132
|
getCurrentUser(): Promise<any>;
|
|
90
133
|
/**
|
|
91
|
-
* Extract page ID from Confluence URL
|
|
92
|
-
* Supports formats like:
|
|
93
|
-
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title
|
|
94
|
-
* - https://your-domain.atlassian.net/wiki/pages/viewpage.action?pageId=123456
|
|
95
|
-
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/overview?homepageId=123456
|
|
134
|
+
* Extract page ID from Confluence URL (see `extractPageIdFromUrl`).
|
|
96
135
|
*/
|
|
97
136
|
static extractPageIdFromUrl(url: string): string | null;
|
|
137
|
+
/**
|
|
138
|
+
* Extract folder ID from Confluence URL (see `extractFolderIdFromUrl`).
|
|
139
|
+
*/
|
|
140
|
+
static extractFolderIdFromUrl(url: string): string | null;
|
|
141
|
+
/**
|
|
142
|
+
* Identify the page or folder a Confluence URL points to
|
|
143
|
+
* (see `extractContentFromUrl`).
|
|
144
|
+
*/
|
|
145
|
+
static extractContentFromUrl(url: string): ConfluenceContentRef | null;
|
|
98
146
|
}
|
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import { BasicAuthStrategy, OAuth2Strategy } from "./auth-strategies";
|
|
2
|
+
import { extractPageIdFromUrl, extractFolderIdFromUrl, extractContentFromUrl, } from "../utils/url-parser";
|
|
3
|
+
/** Content type -> REST v2 endpoint segment. */
|
|
4
|
+
const V2_SEGMENTS = {
|
|
5
|
+
page: "pages",
|
|
6
|
+
folder: "folders",
|
|
7
|
+
whiteboard: "whiteboards",
|
|
8
|
+
database: "databases",
|
|
9
|
+
embed: "embeds",
|
|
10
|
+
};
|
|
11
|
+
/** Page size requested for direct-children listings (v2 maximum). */
|
|
12
|
+
const CHILDREN_PAGE_SIZE = 250;
|
|
13
|
+
/** Default cap on the number of children returned by `getDirectChildren`. */
|
|
14
|
+
const DEFAULT_MAX_CHILDREN = 1000;
|
|
15
|
+
/** Confluence content IDs are numeric; anything else is refused before a request. */
|
|
16
|
+
const NUMERIC_ID = /^\d+$/;
|
|
17
|
+
/** Prefix of v2 paths found in `_links.next`, stripped before re-requesting. */
|
|
18
|
+
const V2_PATH_PREFIX = "/wiki/api/v2/";
|
|
2
19
|
export class ConfluenceApiError extends Error {
|
|
3
20
|
constructor(message, statusCode) {
|
|
4
21
|
super(message);
|
|
@@ -55,12 +72,31 @@ export class ConfluenceClient {
|
|
|
55
72
|
}
|
|
56
73
|
return `${this.baseUrl}/wiki/rest/api/${endpoint}`;
|
|
57
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Construct a REST v2 URL whatever the authentication strategy:
|
|
77
|
+
* - Basic Auth: {baseUrl}/wiki/api/v2/{endpoint}
|
|
78
|
+
* - OAuth2: https://api.atlassian.com/ex/confluence/{cloudId}/wiki/api/v2/{endpoint}
|
|
79
|
+
*/
|
|
80
|
+
async getApiUrlV2(endpoint) {
|
|
81
|
+
if (this.authStrategy instanceof OAuth2Strategy) {
|
|
82
|
+
return this.getApiUrl(endpoint); // already v2
|
|
83
|
+
}
|
|
84
|
+
if (!this.baseUrl) {
|
|
85
|
+
throw new Error("Base URL is required for Basic Authentication");
|
|
86
|
+
}
|
|
87
|
+
return `${this.baseUrl}/wiki/api/v2/${endpoint}`;
|
|
88
|
+
}
|
|
58
89
|
/**
|
|
59
90
|
* Make an authenticated request to the Confluence API
|
|
60
91
|
* Handles token refresh on 401 errors for OAuth2
|
|
92
|
+
* @param options.v2 - Force REST v2 (default: v1 in Basic Auth, v2 in OAuth2)
|
|
93
|
+
* @param options.retryCount - Internal: number of retries already done
|
|
61
94
|
*/
|
|
62
|
-
async makeRequest(endpoint,
|
|
63
|
-
const
|
|
95
|
+
async makeRequest(endpoint, options = {}) {
|
|
96
|
+
const { v2 = false, retryCount = 0 } = options;
|
|
97
|
+
const url = v2
|
|
98
|
+
? await this.getApiUrlV2(endpoint)
|
|
99
|
+
: await this.getApiUrl(endpoint);
|
|
64
100
|
const authHeaders = await this.authStrategy.getAuthHeaders();
|
|
65
101
|
const response = await fetch(url, {
|
|
66
102
|
headers: {
|
|
@@ -74,7 +110,7 @@ export class ConfluenceClient {
|
|
|
74
110
|
if (response.status === 401 && this.authStrategy.refreshAuth && retryCount === 0) {
|
|
75
111
|
await this.authStrategy.refreshAuth();
|
|
76
112
|
// Retry request with new token (only once)
|
|
77
|
-
return this.makeRequest(endpoint, retryCount + 1);
|
|
113
|
+
return this.makeRequest(endpoint, { v2, retryCount: retryCount + 1 });
|
|
78
114
|
}
|
|
79
115
|
if (!response.ok) {
|
|
80
116
|
const errorBody = await response.text();
|
|
@@ -84,6 +120,7 @@ export class ConfluenceClient {
|
|
|
84
120
|
return data;
|
|
85
121
|
}
|
|
86
122
|
async getPage(pageId, includeChildren = false) {
|
|
123
|
+
this.assertNumericId(pageId, "page ID");
|
|
87
124
|
// OAuth2 uses API v2, Basic Auth uses API v1
|
|
88
125
|
if (this.authStrategy instanceof OAuth2Strategy) {
|
|
89
126
|
// API v2 endpoint: /pages/{id}
|
|
@@ -156,6 +193,7 @@ export class ConfluenceClient {
|
|
|
156
193
|
}
|
|
157
194
|
}
|
|
158
195
|
async getChildPages(pageId) {
|
|
196
|
+
this.assertNumericId(pageId, "page ID");
|
|
159
197
|
// OAuth2 uses API v2, Basic Auth uses API v1
|
|
160
198
|
if (this.authStrategy instanceof OAuth2Strategy) {
|
|
161
199
|
// API v2 endpoint: /pages/{id}/children
|
|
@@ -182,6 +220,141 @@ export class ConfluenceClient {
|
|
|
182
220
|
return response.results;
|
|
183
221
|
}
|
|
184
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Map a content type to its REST v2 endpoint segment.
|
|
225
|
+
* Throws without any network call for unsupported types.
|
|
226
|
+
*/
|
|
227
|
+
v2Segment(type) {
|
|
228
|
+
// `hasOwnProperty` keeps inherited keys (`constructor`, `__proto__`...) out.
|
|
229
|
+
if (typeof type !== "string" || !Object.prototype.hasOwnProperty.call(V2_SEGMENTS, type)) {
|
|
230
|
+
throw new ConfluenceApiError(`Unsupported content type: ${String(type)}`);
|
|
231
|
+
}
|
|
232
|
+
return V2_SEGMENTS[type];
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Ensure an ID is purely numeric before it is interpolated into a URL path.
|
|
236
|
+
* Without this, `..` segments would be resolved by the URL parser and could
|
|
237
|
+
* reach other endpoints on the credentialed host. No request is sent.
|
|
238
|
+
*/
|
|
239
|
+
assertNumericId(id, label) {
|
|
240
|
+
if (typeof id !== "string" || !NUMERIC_ID.test(id)) {
|
|
241
|
+
throw new ConfluenceApiError(`Invalid ${label}: must be a numeric ID`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Read a folder (REST v2). The space is resolved with a second call;
|
|
246
|
+
* `space` is `null` when that lookup fails, never the space ID.
|
|
247
|
+
*/
|
|
248
|
+
async getFolder(id) {
|
|
249
|
+
this.assertNumericId(id, "folder ID");
|
|
250
|
+
const data = await this.makeRequest(`folders/${id}`, { v2: true });
|
|
251
|
+
let space = null;
|
|
252
|
+
if (data.spaceId) {
|
|
253
|
+
try {
|
|
254
|
+
const spaceData = await this.makeRequest(`spaces/${data.spaceId}`, {
|
|
255
|
+
v2: true,
|
|
256
|
+
});
|
|
257
|
+
if (spaceData?.key) {
|
|
258
|
+
space = { key: spaceData.key, name: spaceData.name };
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
space = null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
id: data.id,
|
|
267
|
+
title: data.title,
|
|
268
|
+
type: "folder",
|
|
269
|
+
spaceId: data.spaceId,
|
|
270
|
+
parentId: data.parentId,
|
|
271
|
+
parentType: data.parentType,
|
|
272
|
+
status: data.status,
|
|
273
|
+
space,
|
|
274
|
+
_links: { webui: data._links?.webui ?? "" },
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* List the direct children of a page or folder (REST v2), following
|
|
279
|
+
* cursor pagination. Stops once `maxItems` (default 1000) items are
|
|
280
|
+
* collected and reports it with `truncated`.
|
|
281
|
+
*/
|
|
282
|
+
async getDirectChildren(parentType, id, options = {}) {
|
|
283
|
+
const maxItems = options.maxItems ?? DEFAULT_MAX_CHILDREN;
|
|
284
|
+
if (!Number.isInteger(maxItems) || maxItems < 1) {
|
|
285
|
+
throw new ConfluenceApiError("Invalid maxItems: must be an integer >= 1");
|
|
286
|
+
}
|
|
287
|
+
const segment = this.v2Segment(parentType);
|
|
288
|
+
this.assertNumericId(id, "content ID");
|
|
289
|
+
// Safety net against a server returning `next` forever (e.g. with empty
|
|
290
|
+
// pages): enough requests to fill `maxItems` with full pages, plus one.
|
|
291
|
+
const maxRequests = Math.ceil(maxItems / CHILDREN_PAGE_SIZE) + 1;
|
|
292
|
+
let requests = 0;
|
|
293
|
+
const items = [];
|
|
294
|
+
let endpoint = `${segment}/${id}/direct-children?limit=${CHILDREN_PAGE_SIZE}`;
|
|
295
|
+
while (endpoint) {
|
|
296
|
+
if (requests >= maxRequests) {
|
|
297
|
+
return { items: items.slice(0, maxItems), truncated: true };
|
|
298
|
+
}
|
|
299
|
+
requests++;
|
|
300
|
+
const page = await this.makeRequest(endpoint, { v2: true });
|
|
301
|
+
for (const child of page.results ?? []) {
|
|
302
|
+
items.push({
|
|
303
|
+
id: child.id,
|
|
304
|
+
title: child.title,
|
|
305
|
+
type: child.type,
|
|
306
|
+
status: child.status,
|
|
307
|
+
childPosition: child.childPosition,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
// `_links.next` is a full v2 path already carrying cursor and limit.
|
|
311
|
+
const next = page._links?.next;
|
|
312
|
+
if (items.length >= maxItems) {
|
|
313
|
+
// Truncated only if something remains beyond the cap.
|
|
314
|
+
return {
|
|
315
|
+
items: items.slice(0, maxItems),
|
|
316
|
+
truncated: items.length > maxItems || Boolean(next),
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
if (!next) {
|
|
320
|
+
endpoint = null;
|
|
321
|
+
}
|
|
322
|
+
else if (next.startsWith(V2_PATH_PREFIX)) {
|
|
323
|
+
endpoint = next.slice(V2_PATH_PREFIX.length);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
// Unexpected link shape: never follow it, report what we have.
|
|
327
|
+
return { items, truncated: true };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return { items, truncated: false };
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* List the ancestors of a content item (REST v2), root first as returned.
|
|
334
|
+
*/
|
|
335
|
+
async getAncestors(type, id) {
|
|
336
|
+
const segment = this.v2Segment(type);
|
|
337
|
+
this.assertNumericId(id, "content ID");
|
|
338
|
+
const data = await this.makeRequest(`${segment}/${id}/ancestors`, { v2: true });
|
|
339
|
+
return (data.results ?? []).map((a) => ({ id: a.id, type: a.type }));
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Read the metadata of a content item without its body (REST v2).
|
|
343
|
+
* `type` is the one asked for, not read from the response body.
|
|
344
|
+
*/
|
|
345
|
+
async getContentSummary(type, id) {
|
|
346
|
+
const segment = this.v2Segment(type);
|
|
347
|
+
this.assertNumericId(id, "content ID");
|
|
348
|
+
const data = await this.makeRequest(`${segment}/${id}`, { v2: true });
|
|
349
|
+
return {
|
|
350
|
+
id: data.id,
|
|
351
|
+
title: data.title,
|
|
352
|
+
type,
|
|
353
|
+
spaceId: data.spaceId,
|
|
354
|
+
parentId: data.parentId,
|
|
355
|
+
parentType: data.parentType,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
185
358
|
async getCurrentUser() {
|
|
186
359
|
// OAuth2 uses API v2, Basic Auth uses API v1
|
|
187
360
|
if (this.authStrategy instanceof OAuth2Strategy) {
|
|
@@ -195,28 +368,22 @@ export class ConfluenceClient {
|
|
|
195
368
|
}
|
|
196
369
|
}
|
|
197
370
|
/**
|
|
198
|
-
* Extract page ID from Confluence URL
|
|
199
|
-
* Supports formats like:
|
|
200
|
-
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title
|
|
201
|
-
* - https://your-domain.atlassian.net/wiki/pages/viewpage.action?pageId=123456
|
|
202
|
-
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/overview?homepageId=123456
|
|
371
|
+
* Extract page ID from Confluence URL (see `extractPageIdFromUrl`).
|
|
203
372
|
*/
|
|
204
373
|
static extractPageIdFromUrl(url) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
return null;
|
|
374
|
+
return extractPageIdFromUrl(url);
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Extract folder ID from Confluence URL (see `extractFolderIdFromUrl`).
|
|
378
|
+
*/
|
|
379
|
+
static extractFolderIdFromUrl(url) {
|
|
380
|
+
return extractFolderIdFromUrl(url);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Identify the page or folder a Confluence URL points to
|
|
384
|
+
* (see `extractContentFromUrl`).
|
|
385
|
+
*/
|
|
386
|
+
static extractContentFromUrl(url) {
|
|
387
|
+
return extractContentFromUrl(url);
|
|
221
388
|
}
|
|
222
389
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the Confluence content hierarchy (folders, pages and other
|
|
3
|
+
* content kinds) returned by the REST v2 read methods of `ConfluenceClient`.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Kind of a Confluence content item. Known kinds are listed for
|
|
7
|
+
* autocompletion; any other string is accepted because Confluence may add
|
|
8
|
+
* new kinds (reads of unsupported kinds are rejected by the client).
|
|
9
|
+
*/
|
|
10
|
+
export type ConfluenceContentType = "page" | "folder" | "whiteboard" | "database" | "embed" | (string & {});
|
|
11
|
+
/** Lightweight item of a direct-children listing (no body, no space). */
|
|
12
|
+
export interface ConfluenceTreeItem {
|
|
13
|
+
id: string;
|
|
14
|
+
title: string;
|
|
15
|
+
type: ConfluenceContentType;
|
|
16
|
+
status: string;
|
|
17
|
+
/** Position among siblings, as ordered in the Confluence page tree. */
|
|
18
|
+
childPosition?: number;
|
|
19
|
+
}
|
|
20
|
+
/** Ancestor reference; ancestors are returned root first. */
|
|
21
|
+
export interface ConfluenceAncestor {
|
|
22
|
+
id: string;
|
|
23
|
+
type: ConfluenceContentType;
|
|
24
|
+
}
|
|
25
|
+
/** Metadata of a content item, without its body. */
|
|
26
|
+
export interface ConfluenceContentSummary {
|
|
27
|
+
id: string;
|
|
28
|
+
title: string;
|
|
29
|
+
type: ConfluenceContentType;
|
|
30
|
+
spaceId: string;
|
|
31
|
+
parentId?: string;
|
|
32
|
+
parentType?: ConfluenceContentType;
|
|
33
|
+
}
|
|
34
|
+
/** A Confluence folder, with its space resolved when permissions allow. */
|
|
35
|
+
export interface ConfluenceFolder extends ConfluenceContentSummary {
|
|
36
|
+
status: string;
|
|
37
|
+
/**
|
|
38
|
+
* Space of the folder, or `null` when the space lookup failed (for example
|
|
39
|
+
* missing permission). Never filled with the space ID, so callers can rely
|
|
40
|
+
* on `space.key` for whitelist checks.
|
|
41
|
+
*/
|
|
42
|
+
space: {
|
|
43
|
+
key: string;
|
|
44
|
+
name: string;
|
|
45
|
+
} | null;
|
|
46
|
+
_links: {
|
|
47
|
+
webui: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** Result of a direct-children listing. */
|
|
51
|
+
export interface DirectChildrenResult {
|
|
52
|
+
items: ConfluenceTreeItem[];
|
|
53
|
+
/** `true` when more children exist than the requested `maxItems`. */
|
|
54
|
+
truncated: boolean;
|
|
55
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export { ConfluenceClient, ConfluenceApiError } from "./client/confluence-client";
|
|
2
|
+
export type { ConfluenceContentType, ConfluenceTreeItem, ConfluenceAncestor, ConfluenceContentSummary, ConfluenceFolder, DirectChildrenResult, } from "./client/content-types";
|
|
2
3
|
export { BasicAuthStrategy, OAuth2Strategy, type AuthStrategy, } from "./client/auth-strategies";
|
|
3
4
|
export { processADFWithMedia } from "./processors/media-processor";
|
|
4
5
|
export { processADFWithTOC } from "./processors/toc-processor";
|
|
5
6
|
export { processADFWithLinks, type EnrichedLinkData, type LinkProcessorOptions, } from "./processors/link-processor";
|
|
6
7
|
export { processADF, extractTextFromADFNodes, generateSlug, } from "./processors/adf-processor";
|
|
7
8
|
export { UrlTransformer } from "./utils/url-transformer";
|
|
9
|
+
export { extractPageIdFromUrl, extractFolderIdFromUrl, extractContentFromUrl, type ConfluenceContentRef, } from "./utils/url-parser";
|
|
8
10
|
export * from "./types";
|
package/dist/index.js
CHANGED
|
@@ -10,5 +10,6 @@ export { processADFWithLinks, } from "./processors/link-processor";
|
|
|
10
10
|
export { processADF, extractTextFromADFNodes, generateSlug, } from "./processors/adf-processor";
|
|
11
11
|
// Utilities
|
|
12
12
|
export { UrlTransformer } from "./utils/url-transformer";
|
|
13
|
+
export { extractPageIdFromUrl, extractFolderIdFromUrl, extractContentFromUrl, } from "./utils/url-parser";
|
|
13
14
|
// Types
|
|
14
15
|
export * from "./types";
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { ADFNode } from "../types";
|
|
2
|
-
import { ConfluenceClient } from "../client/confluence-client";
|
|
2
|
+
import type { ConfluenceClient } from "../client/confluence-client";
|
|
3
3
|
/**
|
|
4
|
-
* Link enrichment result containing metadata about a Confluence page link
|
|
4
|
+
* Link enrichment result containing metadata about a Confluence page or folder link
|
|
5
5
|
*/
|
|
6
6
|
export interface EnrichedLinkData {
|
|
7
|
+
/**
|
|
8
|
+
* ID of the linked content. The name is kept for backward compatibility:
|
|
9
|
+
* it holds a folder ID when `contentType` is "folder".
|
|
10
|
+
*/
|
|
7
11
|
pageId: string;
|
|
12
|
+
/** Kind of content the link points to */
|
|
13
|
+
contentType: "page" | "folder";
|
|
8
14
|
title: string;
|
|
9
15
|
originalUrl: string;
|
|
10
16
|
}
|
|
@@ -16,13 +22,13 @@ export interface LinkProcessorOptions {
|
|
|
16
22
|
confluenceBaseUrl: string;
|
|
17
23
|
}
|
|
18
24
|
/**
|
|
19
|
-
* Process ADF to enrich Confluence page links with
|
|
25
|
+
* Process ADF to enrich Confluence page and folder links with titles
|
|
20
26
|
*
|
|
21
27
|
* This processor:
|
|
22
28
|
* 1. Recursively traverses the ADF tree
|
|
23
29
|
* 2. Finds all links (marks and inlineCard nodes) pointing to Confluence pages
|
|
24
|
-
* 3. Extracts page
|
|
25
|
-
* 4. Fetches
|
|
30
|
+
* 3. Extracts content type and ID (page or folder) from URLs
|
|
31
|
+
* 4. Fetches titles from Confluence API (pages via getPage, folders via getContentSummary)
|
|
26
32
|
* 5. Enriches link nodes with page metadata
|
|
27
33
|
*
|
|
28
34
|
* @param adf - The ADF document to process
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { extractContentFromUrl } from "../utils/url-parser";
|
|
2
2
|
/**
|
|
3
|
-
* Process ADF to enrich Confluence page links with
|
|
3
|
+
* Process ADF to enrich Confluence page and folder links with titles
|
|
4
4
|
*
|
|
5
5
|
* This processor:
|
|
6
6
|
* 1. Recursively traverses the ADF tree
|
|
7
7
|
* 2. Finds all links (marks and inlineCard nodes) pointing to Confluence pages
|
|
8
|
-
* 3. Extracts page
|
|
9
|
-
* 4. Fetches
|
|
8
|
+
* 3. Extracts content type and ID (page or folder) from URLs
|
|
9
|
+
* 4. Fetches titles from Confluence API (pages via getPage, folders via getContentSummary)
|
|
10
10
|
* 5. Enriches link nodes with page metadata
|
|
11
11
|
*
|
|
12
12
|
* @param adf - The ADF document to process
|
|
@@ -78,50 +78,52 @@ function isConfluenceUrl(url, confluenceBaseUrl) {
|
|
|
78
78
|
if (!url.startsWith(normalizedBase)) {
|
|
79
79
|
return false;
|
|
80
80
|
}
|
|
81
|
-
// Check if we can extract a page
|
|
82
|
-
|
|
83
|
-
return pageId !== null;
|
|
81
|
+
// Check if we can extract a page or folder reference
|
|
82
|
+
return extractContentFromUrl(url) !== null;
|
|
84
83
|
}
|
|
85
84
|
catch {
|
|
86
85
|
return false;
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
88
|
/**
|
|
90
|
-
* Fetch
|
|
89
|
+
* Fetch titles for a set of Confluence page and folder URLs
|
|
91
90
|
*
|
|
92
|
-
* @param urls - Set of Confluence page URLs
|
|
91
|
+
* @param urls - Set of Confluence page or folder URLs
|
|
93
92
|
* @param client - Confluence API client
|
|
94
93
|
* @returns Array of enriched link data
|
|
95
94
|
*/
|
|
96
95
|
async function fetchPageTitles(urls, client) {
|
|
97
96
|
const results = [];
|
|
98
|
-
// Group URLs by
|
|
99
|
-
const
|
|
97
|
+
// Group URLs by content (type + ID) to avoid duplicate API calls
|
|
98
|
+
const contentMap = new Map();
|
|
100
99
|
for (const url of urls) {
|
|
101
|
-
const
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
100
|
+
const ref = extractContentFromUrl(url);
|
|
101
|
+
if (!ref)
|
|
102
|
+
continue;
|
|
103
|
+
const key = `${ref.type}:${ref.id}`;
|
|
104
|
+
const entry = contentMap.get(key);
|
|
105
|
+
if (entry) {
|
|
106
|
+
entry.urls.push(url);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
contentMap.set(key, { ...ref, urls: [url] });
|
|
107
110
|
}
|
|
108
111
|
}
|
|
109
|
-
// Fetch
|
|
110
|
-
await Promise.all(Array.from(
|
|
112
|
+
// Fetch the title of each unique content item
|
|
113
|
+
await Promise.all(Array.from(contentMap.values()).map(async ({ type, id, urls: contentUrls }) => {
|
|
111
114
|
try {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
});
|
|
115
|
+
// Pages keep their historical lookup; folders use the lightweight summary
|
|
116
|
+
const title = type === "page"
|
|
117
|
+
? (await client.getPage(id, false)).title
|
|
118
|
+
: (await client.getContentSummary("folder", id)).title;
|
|
119
|
+
// Add enriched data for all URLs pointing to this content
|
|
120
|
+
for (const url of contentUrls) {
|
|
121
|
+
results.push({ pageId: id, contentType: type, title, originalUrl: url });
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
124
|
catch (error) {
|
|
123
|
-
// If we can't fetch the
|
|
124
|
-
console.warn(`Failed to fetch
|
|
125
|
+
// If we can't fetch the content, skip enrichment for these URLs
|
|
126
|
+
console.warn(`Failed to fetch ${type} ${id}:`, error);
|
|
125
127
|
}
|
|
126
128
|
}));
|
|
127
129
|
return results;
|
|
@@ -144,6 +146,7 @@ function enrichAdfWithLinkData(node, enrichedLinks) {
|
|
|
144
146
|
...clonedNode.attrs,
|
|
145
147
|
title: enrichedData.title,
|
|
146
148
|
pageId: enrichedData.pageId,
|
|
149
|
+
contentType: enrichedData.contentType,
|
|
147
150
|
};
|
|
148
151
|
}
|
|
149
152
|
}
|
|
@@ -159,6 +162,7 @@ function enrichAdfWithLinkData(node, enrichedLinks) {
|
|
|
159
162
|
...mark.attrs,
|
|
160
163
|
title: enrichedData.title,
|
|
161
164
|
pageId: enrichedData.pageId,
|
|
165
|
+
contentType: enrichedData.contentType,
|
|
162
166
|
},
|
|
163
167
|
};
|
|
164
168
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure URL parsing helpers for Confluence content (pages and folders).
|
|
3
|
+
* Kept free of any client or network dependency so they can be reused anywhere.
|
|
4
|
+
*/
|
|
5
|
+
/** Reference to a piece of Confluence content identified from a URL. */
|
|
6
|
+
export type ConfluenceContentRef = {
|
|
7
|
+
type: "page" | "folder";
|
|
8
|
+
id: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Extract a page ID from a Confluence URL.
|
|
12
|
+
* Supports formats like:
|
|
13
|
+
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title
|
|
14
|
+
* - https://your-domain.atlassian.net/wiki/pages/viewpage.action?pageId=123456
|
|
15
|
+
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/overview?homepageId=123456
|
|
16
|
+
*
|
|
17
|
+
* @param url - Confluence URL
|
|
18
|
+
* @returns The page ID, or null when none can be found
|
|
19
|
+
*/
|
|
20
|
+
export declare function extractPageIdFromUrl(url: string): string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Extract a folder ID from a Confluence URL such as
|
|
23
|
+
* https://your-domain.atlassian.net/wiki/spaces/SPACE/folder/123456/Title.
|
|
24
|
+
* The ID must be followed by a separator or the end of the URL, so a
|
|
25
|
+
* "/folder/" fragment inside a page title is not mistaken for a folder.
|
|
26
|
+
*
|
|
27
|
+
* @param url - Confluence URL
|
|
28
|
+
* @returns The folder ID, or null when none can be found
|
|
29
|
+
*/
|
|
30
|
+
export declare function extractFolderIdFromUrl(url: string): string | null;
|
|
31
|
+
/**
|
|
32
|
+
* Identify the content a Confluence URL points to.
|
|
33
|
+
* Folders are checked first, then pages.
|
|
34
|
+
*
|
|
35
|
+
* @param url - Confluence URL
|
|
36
|
+
* @returns A typed content reference, or null when the URL has no content ID
|
|
37
|
+
*/
|
|
38
|
+
export declare function extractContentFromUrl(url: string): ConfluenceContentRef | null;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure URL parsing helpers for Confluence content (pages and folders).
|
|
3
|
+
* Kept free of any client or network dependency so they can be reused anywhere.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Extract a page ID from a Confluence URL.
|
|
7
|
+
* Supports formats like:
|
|
8
|
+
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/pages/123456/Page+Title
|
|
9
|
+
* - https://your-domain.atlassian.net/wiki/pages/viewpage.action?pageId=123456
|
|
10
|
+
* - https://your-domain.atlassian.net/wiki/spaces/SPACE/overview?homepageId=123456
|
|
11
|
+
*
|
|
12
|
+
* @param url - Confluence URL
|
|
13
|
+
* @returns The page ID, or null when none can be found
|
|
14
|
+
*/
|
|
15
|
+
export function extractPageIdFromUrl(url) {
|
|
16
|
+
// Pattern 1: /pages/123456/title format
|
|
17
|
+
const match1 = url.match(/\/pages\/(\d+)(?:\/|$)/);
|
|
18
|
+
if (match1) {
|
|
19
|
+
return match1[1];
|
|
20
|
+
}
|
|
21
|
+
// Pattern 2: pageId=123456 query parameter
|
|
22
|
+
const match2 = url.match(/[?&]pageId=(\d+)/);
|
|
23
|
+
if (match2) {
|
|
24
|
+
return match2[1];
|
|
25
|
+
}
|
|
26
|
+
// Pattern 3: homepageId=123456 query parameter (space overview pages)
|
|
27
|
+
const match3 = url.match(/[?&]homepageId=(\d+)/);
|
|
28
|
+
if (match3) {
|
|
29
|
+
return match3[1];
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Extract a folder ID from a Confluence URL such as
|
|
35
|
+
* https://your-domain.atlassian.net/wiki/spaces/SPACE/folder/123456/Title.
|
|
36
|
+
* The ID must be followed by a separator or the end of the URL, so a
|
|
37
|
+
* "/folder/" fragment inside a page title is not mistaken for a folder.
|
|
38
|
+
*
|
|
39
|
+
* @param url - Confluence URL
|
|
40
|
+
* @returns The folder ID, or null when none can be found
|
|
41
|
+
*/
|
|
42
|
+
export function extractFolderIdFromUrl(url) {
|
|
43
|
+
const match = url.match(/\/folder\/(\d+)(?:[/?#]|$)/);
|
|
44
|
+
return match ? match[1] : null;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Identify the content a Confluence URL points to.
|
|
48
|
+
* Folders are checked first, then pages.
|
|
49
|
+
*
|
|
50
|
+
* @param url - Confluence URL
|
|
51
|
+
* @returns A typed content reference, or null when the URL has no content ID
|
|
52
|
+
*/
|
|
53
|
+
export function extractContentFromUrl(url) {
|
|
54
|
+
const folderId = extractFolderIdFromUrl(url);
|
|
55
|
+
if (folderId) {
|
|
56
|
+
return { type: "folder", id: folderId };
|
|
57
|
+
}
|
|
58
|
+
const pageId = extractPageIdFromUrl(url);
|
|
59
|
+
if (pageId) {
|
|
60
|
+
return { type: "page", id: pageId };
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
@@ -40,7 +40,7 @@ export declare class UrlTransformer {
|
|
|
40
40
|
*
|
|
41
41
|
* Returns true if:
|
|
42
42
|
* 1. The URL points to the configured Confluence instance
|
|
43
|
-
* 2. A page ID can be extracted from the URL
|
|
43
|
+
* 2. A page or folder ID can be extracted from the URL
|
|
44
44
|
*
|
|
45
45
|
* @param url - URL to check
|
|
46
46
|
* @returns true if URL should be transformed
|
|
@@ -49,11 +49,11 @@ export declare class UrlTransformer {
|
|
|
49
49
|
/**
|
|
50
50
|
* Transform a Confluence URL to a local viewer URL
|
|
51
51
|
*
|
|
52
|
-
* Extracts the page ID from the Confluence URL and creates a
|
|
53
|
-
*
|
|
52
|
+
* Extracts the page or folder ID from the Confluence URL and creates a
|
|
53
|
+
* local URL with it as a query parameter (`pageId` or `folderId`).
|
|
54
54
|
*
|
|
55
|
-
* @param confluenceUrl - Confluence page URL to transform
|
|
56
|
-
* @returns Local viewer URL
|
|
55
|
+
* @param confluenceUrl - Confluence page or folder URL to transform
|
|
56
|
+
* @returns Local viewer URL, or original URL if transformation fails
|
|
57
57
|
*/
|
|
58
58
|
toLocalUrl(confluenceUrl: string): string;
|
|
59
59
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { extractContentFromUrl } from "./url-parser";
|
|
2
2
|
/**
|
|
3
3
|
* Transform Confluence URLs to local viewer URLs
|
|
4
4
|
*
|
|
@@ -39,7 +39,7 @@ export class UrlTransformer {
|
|
|
39
39
|
*
|
|
40
40
|
* Returns true if:
|
|
41
41
|
* 1. The URL points to the configured Confluence instance
|
|
42
|
-
* 2. A page ID can be extracted from the URL
|
|
42
|
+
* 2. A page or folder ID can be extracted from the URL
|
|
43
43
|
*
|
|
44
44
|
* @param url - URL to check
|
|
45
45
|
* @returns true if URL should be transformed
|
|
@@ -50,9 +50,8 @@ export class UrlTransformer {
|
|
|
50
50
|
if (!url.startsWith(this.confluenceBaseUrl)) {
|
|
51
51
|
return false;
|
|
52
52
|
}
|
|
53
|
-
// Check if we can extract a page ID
|
|
54
|
-
|
|
55
|
-
return pageId !== null;
|
|
53
|
+
// Check if we can extract a page or folder ID
|
|
54
|
+
return extractContentFromUrl(url) !== null;
|
|
56
55
|
}
|
|
57
56
|
catch {
|
|
58
57
|
return false;
|
|
@@ -61,21 +60,22 @@ export class UrlTransformer {
|
|
|
61
60
|
/**
|
|
62
61
|
* Transform a Confluence URL to a local viewer URL
|
|
63
62
|
*
|
|
64
|
-
* Extracts the page ID from the Confluence URL and creates a
|
|
65
|
-
*
|
|
63
|
+
* Extracts the page or folder ID from the Confluence URL and creates a
|
|
64
|
+
* local URL with it as a query parameter (`pageId` or `folderId`).
|
|
66
65
|
*
|
|
67
|
-
* @param confluenceUrl - Confluence page URL to transform
|
|
68
|
-
* @returns Local viewer URL
|
|
66
|
+
* @param confluenceUrl - Confluence page or folder URL to transform
|
|
67
|
+
* @returns Local viewer URL, or original URL if transformation fails
|
|
69
68
|
*/
|
|
70
69
|
toLocalUrl(confluenceUrl) {
|
|
71
70
|
if (!this.shouldTransform(confluenceUrl)) {
|
|
72
71
|
return confluenceUrl;
|
|
73
72
|
}
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
73
|
+
const content = extractContentFromUrl(confluenceUrl);
|
|
74
|
+
if (!content) {
|
|
76
75
|
return confluenceUrl;
|
|
77
76
|
}
|
|
78
|
-
|
|
77
|
+
const param = content.type === "folder" ? "folderId" : "pageId";
|
|
78
|
+
return `${this.localBaseUrl}/?${param}=${content.id}`;
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
81
|
* Batch transform multiple URLs
|
package/package.json
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gravitek/confluence-mirror-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Core logic for Confluence page rendering - API client, ADF processing, media handling. Unofficial, not affiliated with Atlassian.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"dist"
|
|
8
|
+
"dist",
|
|
9
|
+
"NOTICE"
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
11
12
|
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
12
14
|
"dev": "tsc --watch",
|
|
13
|
-
"clean": "rm -rf dist"
|
|
15
|
+
"clean": "rm -rf dist",
|
|
16
|
+
"test": "vitest run"
|
|
14
17
|
},
|
|
15
18
|
"keywords": [
|
|
16
19
|
"confluence",
|
|
@@ -28,11 +31,15 @@
|
|
|
28
31
|
},
|
|
29
32
|
"homepage": "https://github.com/Gravitek-io/confluence-mirror.git",
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"typescript": "^
|
|
32
|
-
"@types/node": "^
|
|
34
|
+
"typescript": "^6",
|
|
35
|
+
"@types/node": "^22",
|
|
36
|
+
"vitest": "^5"
|
|
33
37
|
},
|
|
34
38
|
"dependencies": {},
|
|
35
39
|
"publishConfig": {
|
|
36
40
|
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22"
|
|
37
44
|
}
|
|
38
45
|
}
|