θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content
51 changes: 28 additions & 23 deletions collector/utils/extensions/Confluence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,34 @@ const {
ConfluencePagesLoader,
} = require("langchain/document_loaders/web/confluence");

function urlMatchesPatter(url, patter) {
const urlPattern = new UrlPattern(patter);
return urlPattern.match(url);
}

function generateCustomDomain({ subdomain, domain, tld }) {
return (subdomain ? `${subdomain}.` : "") + `${domain}.${tld}`;
}

function validSpaceUrl(spaceUrl = "") {
// Atlassian default URL match
const atlassianPattern = new UrlPattern(
"https\\://(:subdomain).atlassian.net/wiki/spaces/(:spaceKey)*"
);
const atlassianMatch = atlassianPattern.match(spaceUrl);
const atlassianMatch = urlMatchesPatter(spaceUrl, "https\\://(:subdomain).atlassian.net/wiki/spaces/(:spaceKey)*");
if (atlassianMatch) {
return { valid: true, result: atlassianMatch };
}

let customMatch = null;
[
"https\\://(:subdomain.):domain.:tld/wiki/spaces/(:spaceKey)*", // Custom Confluence space
"https\\://(:subdomain.):domain.:tld/display/(:spaceKey)*", // Custom Confluence space + Human-readable space tag.
].forEach((matchPattern) => {
if (!!customMatch) return;
const pattern = new UrlPattern(matchPattern);
customMatch = pattern.match(spaceUrl);
});

// Custom URL match
const customMatch = urlMatchesPatter(spaceUrl, "https\\://(:subdomain.):domain.:tld/wiki/spaces/(:spaceKey)*");
if (customMatch) {
customMatch.customDomain =
(customMatch.subdomain ? `${customMatch.subdomain}.` : "") + //
`${customMatch.domain}.${customMatch.tld}`;
return { valid: true, result: customMatch, custom: true };
customMatch.customDomain = generateCustomDomain(customMatch);
return { valid: true, result: customMatch, humanReadable: false };
}

// Human Readable URL match
const humanReadableMatch = urlMatchesPatter(spaceUrl, "https\\://(:subdomain.):domain.:tld/display/(:spaceKey)*");
if (humanReadableMatch) {
humanReadableMatch.customDomain = generateCustomDomain(humanReadableMatch);
return { valid: true, result: humanReadableMatch, humanReadable: true };
}

// No match
Expand All @@ -50,18 +53,20 @@ async function loadConfluence({ pageUrl, username, accessToken }) {
}

const validSpace = validSpaceUrl(pageUrl);
if (!validSpace.result) {
const { result: validSpaceResult, humanReadable } = validSpace;
if (!validSpaceResult) {
return {
success: false,
reason:
"Confluence space URL is not in the expected format of https://domain.atlassian.net/wiki/space/~SPACEID/* or https://customDomain/wiki/space/~SPACEID/*",
"Confluence space URL is not in the expected format of one of https://domain.atlassian.net/wiki/space/~SPACEID/* or https://customDomain/wiki/space/~SPACEID/* or https://customDomain/display/~SPACEID/*",
};
}

const { subdomain, customDomain, spaceKey } = validSpace.result;
let baseUrl = `https://${subdomain}.atlassian.net/wiki`;
const { subdomain, customDomain, spaceKey } = validSpaceResult;
let subpath = humanReadable ? `` : `/wiki`;
let baseUrl = `https://${subdomain}.atlassian.net${subpath}`;
if (customDomain) {
baseUrl = `https://${customDomain}/wiki`;
baseUrl = `https://${customDomain}${subpath}`;
}

console.log(`-- Working Confluence ${baseUrl} --`);
Expand Down