Skip to content

Commit

Permalink
feat: Handle smartcards storing URL without page name on the end
Browse files Browse the repository at this point in the history
  • Loading branch information
andymac4182 committed May 5, 2023
1 parent f17dd84 commit d489f83
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
32 changes: 32 additions & 0 deletions packages/lib/src/ConfluenceUrlParser.ts
@@ -0,0 +1,32 @@
export function cleanUpUrlIfConfluence(
input: string,
confluenceBaseUrl: string
): string {
let url: URL;

// Check if the input is a valid URL
try {
url = new URL(input);
} catch {
return "#";
}

const confluenceUrl = new URL(confluenceBaseUrl);
if (url.hostname !== confluenceUrl.hostname) {
return input;
}

// Check if the input matches the specified path format
const pathRegex = /\/wiki\/spaces\/(?:~)?(\w+)\/pages\/(\d+)(?:\/(\w*))?/;
const matches = url.pathname.match(pathRegex);

if (matches) {
// Update the pathname to remove the last optional part
url.pathname = `/wiki/spaces/${matches[1]}/pages/${matches[2]}`;

// Return the updated URL
return url.href;
}

return input;
}
7 changes: 5 additions & 2 deletions packages/lib/src/ConniePageConfig.ts
Expand Up @@ -137,7 +137,7 @@ export const conniePerPageConfig: ConfluencePerPageConfig = {
yamlValue,
markdownFile,
_alreadyParsed,
_settings,
settings,
adfContent
) => {
if (yamlValue && Array.isArray(yamlValue)) {
Expand All @@ -153,7 +153,10 @@ export const conniePerPageConfig: ConfluencePerPageConfig = {
}
}

const newADF = parseMarkdownToADF(frontmatterHeader);
const newADF = parseMarkdownToADF(
frontmatterHeader,
settings.confluenceBaseUrl
);

adfContent.content = [...newADF.content, ...adfContent.content];
}
Expand Down
21 changes: 16 additions & 5 deletions packages/lib/src/MdToADF.ts
Expand Up @@ -11,20 +11,24 @@ import { p } from "@atlaskit/adf-utils/builders";
import { MarkdownToConfluenceCodeBlockLanguageMap } from "./CodeBlockLanguageMap";
import { isSafeUrl } from "@atlaskit/adf-schema";
import { ConfluenceSettings } from "./Settings";
import { cleanUpUrlIfConfluence } from "./ConfluenceUrlParser";

const frontmatterRegex = /^\s*?---\n([\s\S]*?)\n---\s*/g;

const transformer = new MarkdownTransformer();
const serializer = new JSONTransformer();

export function parseMarkdownToADF(markdown: string) {
export function parseMarkdownToADF(
markdown: string,
confluenceBaseUrl: string
) {
const prosenodes = transformer.parse(markdown);
const adfNodes = serializer.encode(prosenodes);
const nodes = processADF(adfNodes);
const nodes = processADF(adfNodes, confluenceBaseUrl);
return nodes;
}

function processADF(adf: JSONDocNode): JSONDocNode {
function processADF(adf: JSONDocNode, confluenceBaseUrl: string): JSONDocNode {
const olivia = traverse(adf, {
text: (node, _parent) => {
if (
Expand Down Expand Up @@ -53,8 +57,12 @@ function processADF(adf: JSONDocNode): JSONDocNode {
}

if (node.marks[0].attrs["href"] === node.text) {
const cleanedUrl = cleanUpUrlIfConfluence(
node.marks[0].attrs["href"],
confluenceBaseUrl
);
node.type = "inlineCard";
node.attrs = { url: node.marks[0].attrs["href"] };
node.attrs = { url: cleanedUrl };
delete node.marks;
delete node.text;
}
Expand Down Expand Up @@ -138,7 +146,10 @@ export function convertMDtoADF(
): LocalAdfFile {
file.contents = file.contents.replace(frontmatterRegex, "");

const adfContent = parseMarkdownToADF(file.contents);
const adfContent = parseMarkdownToADF(
file.contents,
settings.confluenceBaseUrl
);

const results = processConniePerPageConfig(file, settings, adfContent);

Expand Down

0 comments on commit d489f83

Please sign in to comment.