Skip to content

Commit

Permalink
#6 fixed windows attachment separators to /
Browse files Browse the repository at this point in the history
  • Loading branch information
symunona committed Jan 13, 2024
1 parent 0be685e commit 2755d62
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 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.4",
"version": "2.0.6",
"minAppVersion": "0.15.0",
"description": "Use Dataview queries to export a set of notes with assets",
"author": "symunona",
Expand Down
24 changes: 21 additions & 3 deletions src/export/get-markdown-attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { BulkExportSettings } from "src/models/bulk-export-settings";
import { getAssetPaths } from "src/utils/indexing/asset-and-link-paths";
import replaceAll from "src/utils/replace-all";
import normalizeFileName from "src/utils/normalize-file-name";
import { normalizeLinkToForwardSlash } from "src/utils/forward-slash";

export const ATTACHMENT_URL_REGEXP = /!\[\[((.*?)\.(\w+))\]\]/g;
export const MARKDOWN_ATTACHMENT_URL_REGEXP = /!\[(.*?)\]\(((.*?)\.(\w+))\)/g;
Expand Down Expand Up @@ -64,7 +65,16 @@ export function collectAndReplaceHeaderAttachments(

// This is not pretty, but it works.
let frontMatterPart = contentSplitByHrDashes.shift() || ''
frontMatterPart = replaceAll(attachment.originalPath, frontMatterPart, attachment.newPath)
frontMatterPart = replaceAll(
attachment.originalPath,
frontMatterPart,
// Replace with normalized '/' slashes, always. Windows uses (\) backslashes.
// However the markdown standard is '/' - works on Mac and Linux.
// The links should always be / in markdown documents.
// For copying the assets, the plugin uses the system path's join, hence the replaced
// urls. If I normalize it back here, the link will be fixed and the copy will still work.
normalizeLinkToForwardSlash(attachment.newPath))

contentSplitByHrDashes.unshift(frontMatterPart)
exportProperties.outputContent = contentSplitByHrDashes.join('\n---\n')
}
Expand All @@ -83,7 +93,15 @@ export function collectAndReplaceInlineAttachments(
// I have experimented with this a lot.
// @see comments in getLinksAndAttachments.
// I normalized before exportProperties.outputContent to only have []() style links.
exportProperties.outputContent = replaceAll(`](${attachment.originalPath})`, exportProperties.outputContent, `](${attachment.newPath})`)
exportProperties.outputContent = replaceAll(
`](${attachment.originalPath})`,
exportProperties.outputContent,
// Replace with normalized '/' slashes, always. Windows uses (\) backslashes.
// However the markdown standard is '/' - works on Mac and Linux.
// The links should always be / in markdown documents.
// For copying the assets, the plugin uses the system path's join, hence the replaced
// urls. If I normalize it back here, the link will be fixed and the copy will still work.
`](${normalizeLinkToForwardSlash(attachment.newPath || '')})`)
})
}

Expand Down Expand Up @@ -118,7 +136,7 @@ async function saveAttachmentToLocation(
const imageTargetFileName = normalizeFileName(imageNameWithoutExtension) + "-" + imageLinkMd5 + imageExtension;

// Calculate the link within the markdown file, using the target's relative path!
const documentLink = join(toDirRelative, imageTargetFileName);
const documentLink = join(toDirRelative, imageTargetFileName).replace(/\\/g, '/');
attachment.newPath = documentLink;

const assetAbsoluteTarget = join(toDir, imageTargetFileName);
Expand Down
8 changes: 8 additions & 0 deletions src/utils/forward-slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Replaces all the \ slashes with / in a string.
* @param path
* @returns
*/
export function normalizeLinkToForwardSlash(path: string): string{
return path.replace(/\\/g, '/')
}

0 comments on commit 2755d62

Please sign in to comment.