Skip to content

Commit

Permalink
Added Wikilink syntax support.
Browse files Browse the repository at this point in the history
Refined tests, cleaned code.
  • Loading branch information
symunona committed Nov 9, 2023
1 parent b37caf6 commit 743b5d8
Show file tree
Hide file tree
Showing 15 changed files with 8,216 additions and 227 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "bulk-exporter",
"name": "Bulk Exporter",
"version": "2.0.1",
"version": "2.0.2",
"minAppVersion": "0.15.0",
"description": "Use Dataview queries to export a set of notes with assets",
"author": "symunona",
Expand Down
2 changes: 1 addition & 1 deletion src/export/collect-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function collectAssetsReplaceLinks(
) {
const linksAndAttachments = getLinksAndAttachments(fileExportProperties.content)
fileExportProperties.linksAndAttachments = linksAndAttachments
fileExportProperties.content = linksAndAttachments.markdownReplacedWikiStyleLinks
fileExportProperties.outputContent = linksAndAttachments.markdownReplacedWikiStyleLinks

// console.warn(fileExportProperties.newFileName, linksAndAttachments)

Expand Down
6 changes: 4 additions & 2 deletions src/export/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class Exporter {
Object.keys(lastExport).forEach((absoluteFilePath)=>{
const exportProperties = lastExport[absoluteFilePath]
exportProperties.content = "";
exportProperties.outputContent = "";
exportProperties.file = undefined;
})

Expand Down Expand Up @@ -273,15 +274,16 @@ export async function convertAndCopy(
if (!fileDescriptor) { throw new Error('Null Error') }

const fileContent = await plugin.app.vault.adapter.read(fileDescriptor.path);
fileExportProperties.content = fileContent;
fileExportProperties.outputContent = fileExportProperties.content = fileContent;

fileExportProperties.md5 = Md5.hashStr(fileContent);

// This populates fileExportProperties.outputContent
await collectAssetsReplaceLinks(fileExportProperties, allFileListMap, settings, plugin);

writeFileSync(
fileExportProperties.toAbsoluteFs,
fileExportProperties.content,
fileExportProperties.outputContent,
"utf-8"
);

Expand Down
14 changes: 10 additions & 4 deletions src/export/get-links-and-attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getLinksAndAttachments(markdown: string): LinkParseResults {
}

/**
* Instead of trying to hack double bracket into markdown.it parser library, I just pre-process
* Instead of trying to hack double bracket into markdown.it parser library, I juost pre-process
* all the links to standard []() notation.
* @param markdown
* @returns
Expand All @@ -83,9 +83,15 @@ export function replaceDoubleBracketLinks(markdown: string): string {
const results = markdown.match(DOUBLE_BRACKET_LINK_MATCHER)
if (results) {
results.forEach((link) => {
const linkTarget = link.substring(2, link.length - 2)
const standardLinkStyle = `[${linkTarget}](${encodeURIComponent(linkTarget)})`
// console.warn(link, '->', standardLinkStyle)
let linkTarget = link.substring(2, link.length - 2)
let text = linkTarget
// Support for [[link|text]] styled wiki links.
const linkParts = linkTarget.split('|')
if (linkParts.length > 1 && linkParts[0].trim()){
linkTarget = linkParts.shift() || ''
text = linkParts.join('|')
}
const standardLinkStyle = `[${text}](${encodeURIComponent(linkTarget)})`
markdown = replaceAll(link, markdown, standardLinkStyle)
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/export/get-markdown-attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export function collectAndReplaceHeaderAttachments(
// Replace the links in the header.
if (attachment.newPath) {
// Poor man's yaml splitter.
const contentSplitByHrDashes = exportProperties.content.split('\n---\n')
const contentSplitByHrDashes = exportProperties.outputContent.split('\n---\n')

// This is not pretty, but it works.
let frontMatterPart = contentSplitByHrDashes.shift() || ''
frontMatterPart = replaceAll(attachment.originalPath, frontMatterPart, attachment.newPath)
contentSplitByHrDashes.unshift(frontMatterPart)
exportProperties.content = contentSplitByHrDashes.join('\n---\n')
exportProperties.outputContent = contentSplitByHrDashes.join('\n---\n')
}
})
}
Expand All @@ -81,8 +81,8 @@ export function collectAndReplaceInlineAttachments(
saveAttachmentToLocation(plugin, settings, attachment, exportProperties)
// I have experimented with this a lot.
// @see comments in getLinksAndAttachments.
// I normalized before exportProperties.content to only have []() style links.
exportProperties.content = replaceAll(`](${attachment.originalPath})`, exportProperties.content, `](${attachment.newPath})`)
// I normalized before exportProperties.outputContent to only have []() style links.
exportProperties.outputContent = replaceAll(`](${attachment.originalPath})`, exportProperties.outputContent, `](${attachment.newPath})`)
})
}

Expand Down
12 changes: 6 additions & 6 deletions src/export/replace-local-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export function replaceLocalLinks(

if (!linkedDocument) {
link.error = "Internal Link Not Found at all!"
exportProperties.content = replaceAll(
exportProperties.outputContent = replaceAll(
`[${title}](${original})`,
exportProperties.content,
exportProperties.outputContent,
`${title}`
);
warn('Internal link not found! Removing. ', title, original)
Expand All @@ -55,18 +55,18 @@ export function replaceLocalLinks(
}
link.newPath = newLink
const newLinkWithTitle = `[${title}](${newLink})`;
exportProperties.content = replaceAll(
exportProperties.outputContent = replaceAll(
`[${title}](${original})`,
exportProperties.content,
exportProperties.outputContent,
newLinkWithTitle);
} else {
// Removed as it's pointing to a file that's not being exported.
warn("Internal link not found in output, removing!", original, title, path);
link.error = "Internal Link FOUND but not public, removed!"

exportProperties.content = replaceAll(
exportProperties.outputContent = replaceAll(
`[${title}](${original})`,
exportProperties.content,
exportProperties.outputContent,
`${title}`
);
}
Expand Down
1 change: 1 addition & 0 deletions src/models/export-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AttachmentLink, LinkParseResults } from "src/export/get-links-and-attac
export type ExportProperties = {
toRelativeToExportDirRoot: string;
content: string;
outputContent: string;
md5: string;
frontMatter: Record<string, Literal>;
file?: SMarkdownPage,
Expand Down
4 changes: 2 additions & 2 deletions src/test/local-link-replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ test('JEST test', () => {

describe('getLinksAndAttachments', () => {
test('gets all the links from index file', () => {
const { links } = getLinksAndAttachments(indexMd.content)
expect(links.length).toBe(parseInt(indexMd.frontMatter['internalLinks']))
const { internalLinks } = getLinksAndAttachments(indexMd.content)
expect(internalLinks.length).toBe(parseInt(indexMd.frontMatter['internalLinks']))

})

Expand Down
Loading

0 comments on commit 743b5d8

Please sign in to comment.