Skip to content

Commit

Permalink
fix: Handle #hash links better for names that have spaces and handle …
Browse files Browse the repository at this point in the history
…internal links

Fixes: #231
Fixes: #226
  • Loading branch information
andymac4182 committed Apr 30, 2023
1 parent 6b32c0b commit 7ad345a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
20 changes: 17 additions & 3 deletions packages/lib/src/AdfProcessing.ts
Expand Up @@ -47,7 +47,12 @@ export function prepareAdfToUpload(
result.content = [p()];
}

result = processWikilinkToActualLink(result, fileToPageIdMap, settings);
result = processWikilinkToActualLink(
confluenceNode.file.fileName,
result,
fileToPageIdMap,
settings
);

result = mergeTextNodes(result);

Expand Down Expand Up @@ -634,6 +639,7 @@ function extractInlineComments(adf: JSONDocNode) {
}

function processWikilinkToActualLink(
currentFileName: string,
adf: JSONDocNode,
fileToPageIdMap: Record<string, ConfluenceAdfFile>,
settings: ConfluenceSettings
Expand All @@ -650,13 +656,21 @@ function processWikilinkToActualLink(
node.marks[0].attrs.href.startsWith("wikilink")
) {
const wikilinkUrl = new URL(node.marks[0].attrs.href);
const pagename = `${wikilinkUrl.pathname}.md`;

const pagename =
wikilinkUrl.pathname !== ""
? `${wikilinkUrl.pathname}.md`
: currentFileName;
const linkPage = fileToPageIdMap[pagename];

if (linkPage) {
console.log({ wikilinkUrl });
const confluenceUrl = `${settings.confluenceBaseUrl}/wiki/spaces/${linkPage.spaceKey}/pages/${linkPage.pageId}${wikilinkUrl.hash}`;
node.marks[0].attrs.href = confluenceUrl;
if (node.text === wikilinkUrl.pathname) {
if (
node.text ===
`${wikilinkUrl.pathname}${wikilinkUrl.hash}`
) {
node.type = "inlineCard";
node.attrs = {
url: node.marks[0].attrs.href,
Expand Down
27 changes: 23 additions & 4 deletions packages/lib/src/MarkdownTransformer/wikilinks.ts
Expand Up @@ -17,11 +17,21 @@ export function wikilinks(state: StateInline): boolean {
return false;
}

const { hashFragment, headerStart } = findLinkToHeader(
const { hashFragment, headerStart, headerEnd } = findLinkToHeader(
state,
state.pos,
wikiLinkEnd
);

if (hashFragment) {
state.src = replaceBetween(
state.src,
headerStart,
headerEnd,
hashFragment
);
}

const { alias, aliasStart, aliasEnd } = findAlias(
state,
state.pos,
Expand Down Expand Up @@ -56,6 +66,15 @@ export function wikilinks(state: StateInline): boolean {
return true;
}

function replaceBetween(
original: string,
start: number,
end: number,
replacement: string
) {
return original.substring(0, start) + replacement + original.substring(end);
}

function findLinkEnd(state: StateInline, start: number) {
let labelEnd = -1;
let found = false;
Expand Down Expand Up @@ -101,7 +120,7 @@ function findLinkToHeader(

state.pos = start + 2;

while (state.pos < max) {
while (state.pos <= max) {
if (state.src.charCodeAt(state.pos) === 0x23 /* # */) {
foundStart = true;
headerStart = state.pos;
Expand Down Expand Up @@ -130,8 +149,8 @@ function findLinkToHeader(
// restore old state
state.pos = oldPos;

hashFragment = hashFragment?.replace(/\s+/g, "-");
return { hashFragment, headerStart, headerEnd };
const cleanHashFragment = hashFragment?.replace(" ", "-");
return { hashFragment: cleanHashFragment, headerStart, headerEnd };
}

function findAlias(
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid-puppeteer-renderer/src/index.ts
Expand Up @@ -32,7 +32,7 @@ export class PuppeteerMermaidRenderer implements MermaidRenderer {
"..",
"dist",
"mermaid_renderer.html"
); // TODO: Make sure this is copied with CLI
);
const pathToLoad = url.pathToFileURL(mermaidHTMLPath).href;
await page.goto(pathToLoad);

Expand Down
2 changes: 1 addition & 1 deletion packages/obsidian/src/adaptors/obsidian.ts
Expand Up @@ -40,7 +40,7 @@ export default class ObsidianAdaptor implements LoaderAdaptor {
if (!fileFM) {
throw new Error("Missing File in Metadata Cache");
}
const frontMatter = fileFM.frontmatter; //TODO: How to handle this better??
const frontMatter = fileFM.frontmatter;

if (
(file.path.startsWith(this.settings.folderToPublish) &&
Expand Down

0 comments on commit 7ad345a

Please sign in to comment.