Skip to content

Commit

Permalink
Fix #320
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Dec 22, 2023
1 parent 87c84b6 commit cd3dfe0
Showing 1 changed file with 66 additions and 47 deletions.
113 changes: 66 additions & 47 deletions src/bbt/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,75 @@ export async function exportToMarkdown(
}
> = new Map();

const queueRender = async (markdownPath: string, item: any, annots: any[]) => {
if (!toRender.has(markdownPath)) {
const existingMarkdownFile = app.vault.getAbstractFileByPath(
markdownPath
) as TFile;
const existingMarkdown = existingMarkdownFile
? await app.vault.cachedRead(existingMarkdownFile as TFile)
: '';
const existingAnnotations = existingMarkdownFile
? getExistingAnnotations(existingMarkdown)
: '';
const lastImportDate = existingMarkdownFile
? getLastExport(existingMarkdown)
: moment(0);
const isFirstImport = lastImportDate.valueOf() === 0;

const templateData: Record<any, any> = await applyBasicTemplates(
markdownPath,
{
...item,
annotations: annots,

lastImportDate,
isFirstImport,
// legacy
lastExportDate: lastImportDate,
}
);

toRender.set(markdownPath, {
file: existingMarkdownFile,
fileContent: existingMarkdown,
lastImportDate,
existingAnnotations,
templateData,
});
}
}

const getMarkdownPath = async (pathTemplateData: any) => {
return normalizePath(
sanitizeFilePath(
removeStartingSlash(
await renderTemplate(
sourcePath,
exportFormat.outputPathTemplate,
pathTemplateData
)
)
)
);
}

for (let i = 0, len = itemData.length; i < len; i++) {
const item = itemData[i];
const attachments = item.attachments as any[];
const attachmentData = await getAttachmentData(item, database);

if (!attachments.length) {
const pathTemplateData = await applyBasicTemplates(sourcePath, {
annotations: [],
...item,
});
const markdownPath = await getMarkdownPath(pathTemplateData);

await queueRender(markdownPath, item, []);
continue;
}

for (let j = 0, jLen = attachments.length; j < jLen; j++) {
const attachment = attachments[j];
const attachmentPath = attachment.path;
Expand Down Expand Up @@ -626,17 +690,7 @@ export async function exportToMarkdown(
)
: 'image';

const markdownPath = normalizePath(
sanitizeFilePath(
removeStartingSlash(
await renderTemplate(
sourcePath,
exportFormat.outputPathTemplate,
pathTemplateData
)
)
)
);
const markdownPath = await getMarkdownPath(pathTemplateData);

let annots: any[] = [];

Expand Down Expand Up @@ -699,42 +753,7 @@ export async function exportToMarkdown(
attachment.annotations = annots;
}

if (!toRender.has(markdownPath)) {
const existingMarkdownFile = app.vault.getAbstractFileByPath(
markdownPath
) as TFile;
const existingMarkdown = existingMarkdownFile
? await app.vault.cachedRead(existingMarkdownFile as TFile)
: '';
const existingAnnotations = existingMarkdownFile
? getExistingAnnotations(existingMarkdown)
: '';
const lastImportDate = existingMarkdownFile
? getLastExport(existingMarkdown)
: moment(0);
const isFirstImport = lastImportDate.valueOf() === 0;

const templateData: Record<any, any> = await applyBasicTemplates(
markdownPath,
{
...item,
annotations: annots,

lastImportDate,
isFirstImport,
// legacy
lastExportDate: lastImportDate,
}
);

toRender.set(markdownPath, {
file: existingMarkdownFile,
fileContent: existingMarkdown,
lastImportDate,
existingAnnotations,
templateData,
});
}
await queueRender(markdownPath, item, annots);
}
}

Expand Down

0 comments on commit cd3dfe0

Please sign in to comment.