Skip to content

Commit 9748954

Browse files
authored
Upload images (#5)
1 parent 7f4ece5 commit 9748954

File tree

218 files changed

+14218
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+14218
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* Upload images ([#5](https://github.com/Bhacaz/docs-as-code-confluence/pull/5))
56
* Specified node version and updated dependencies ([#4](https://github.com/Bhacaz/docs-as-code-confluence/pull/4))
67

78
## v1 (2022-11-14)

confluence.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,69 @@ class SyncConfluence {
7070
);
7171
});
7272
}
73+
74+
getAttachments(pageId) {
75+
return new Promise((resolve) => {
76+
this.confluenceApi.getAttachments(
77+
this.spaceId,
78+
pageId,
79+
(err, data) => {
80+
if (err) {
81+
console.error(err);
82+
} else {
83+
if (data.results[0]) {
84+
resolve(data.results);
85+
} else {
86+
resolve(undefined);
87+
}
88+
}
89+
}
90+
);
91+
});
92+
}
93+
94+
updateAttachment(pageId, attachmentId, source) {
95+
return new Promise((resolve) => {
96+
this.confluenceApi.updateAttachmentData(
97+
this.spaceId,
98+
pageId,
99+
attachmentId,
100+
source,
101+
(err, data) => {
102+
if (err) {
103+
console.error(err);
104+
} else {
105+
if (data) {
106+
resolve(data);
107+
} else {
108+
resolve(undefined);
109+
}
110+
}
111+
}
112+
);
113+
});
114+
}
115+
116+
uploadAttachment(pageId, source) {
117+
return new Promise((resolve) => {
118+
this.confluenceApi.createAttachment(
119+
this.spaceId,
120+
pageId,
121+
source,
122+
(err, data) => {
123+
if (err) {
124+
console.error(err);
125+
} else {
126+
if (data.results[0]) {
127+
resolve(data.results[0]);
128+
} else {
129+
resolve(undefined);
130+
}
131+
}
132+
}
133+
);
134+
});
135+
}
73136
}
74137

75138
module.exports = SyncConfluence;

index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const filesStructure = require("./files");
33
const SyncConflence = require("./confluence");
44
const markdownToHtml = require("./markdownToHtml");
55
const core = require("@actions/core");
6+
const parser = require("node-html-parser")
7+
const path = require('path')
68

79
const root = "./" + core.getInput("folder", { required: true }) + "/";
810
const spaceId = core.getInput("space-id", { required: true });
@@ -41,6 +43,32 @@ async function findOrCreatePage(pageTitle, parentPageId) {
4143
return pageId;
4244
}
4345

46+
async function uploadAttachment(attachmentSource, pageId) {
47+
attachmentSource = root + attachmentSource;
48+
const existingAttachments = await syncConfluence.getAttachments(pageId)
49+
if (existingAttachments) {
50+
for (let attachment of existingAttachments) {
51+
if (attachment.title === path.basename(attachmentSource)) {
52+
return await syncConfluence.updateAttachment(pageId, attachment.id, attachmentSource);
53+
}
54+
}
55+
}
56+
return await syncConfluence.uploadAttachment(pageId, attachmentSource);
57+
}
58+
59+
async function handleAttachments(contentPageId, data) {
60+
const html = parser.parse(data);
61+
const images = html.querySelectorAll("img")
62+
for (var image of images) {
63+
const attachmentSource = image.getAttribute("src");
64+
// TODO handle remote images
65+
if (attachmentSource.includes("http")) { continue; }
66+
var attachment = await uploadAttachment(attachmentSource.replace("..", "."), contentPageId);
67+
image.replaceWith(parser.parse('<ac:image><ri:attachment ri:filename=' + attachment.title +' /></ac:image>'));
68+
}
69+
return html.toString()
70+
}
71+
4472
async function main() {
4573
for (const f of filesStructure(root)) {
4674
let path = f.join("/");
@@ -52,8 +80,9 @@ async function main() {
5280
pageTitle,
5381
currentParentPageId
5482
);
55-
markdownToHtml(root + path, (err, data) => {
56-
syncConfluence.putContent(contentPageId, pageTitle, data);
83+
markdownToHtml(root + path, (err, data) => {
84+
let htmlContent = handleAttachments(contentPageId, data);
85+
syncConfluence.putContent(contentPageId, pageTitle, htmlContent);
5786
});
5887
} else {
5988
currentParentPageId = await findOrCreatePage(

node_modules/.bin/he

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/boolbase/README.md

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/boolbase/index.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/boolbase/package.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/css-select/LICENSE

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)