Skip to content

Commit

Permalink
chore: fix exporting super notes as md when multiple embedded files h…
Browse files Browse the repository at this point in the history
…ave same name [skip e2e]
  • Loading branch information
amanharwara committed Apr 22, 2024
1 parent 69d1446 commit e455984
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { $createFileExportNode } from '../Lexical/Nodes/FileExportNode'
import { $createInlineFileNode } from '../Plugins/InlineFilePlugin/InlineFileNode'
import { $convertFromMarkdownString } from '../Lexical/Utils/MarkdownImport'
import { $convertToMarkdownString } from '../Lexical/Utils/MarkdownExport'
import { parseFileName } from '@standardnotes/filepicker'

export class HeadlessSuperConverter implements SuperConverterServiceInterface {
private importEditor: LexicalEditor
Expand Down Expand Up @@ -90,6 +91,7 @@ export class HeadlessSuperConverter implements SuperConverterServiceInterface {
return
}
const fileNodes = $nodesOfType(FileNode)
const filenameCounts: Record<string, number> = {}
Promise.all(
fileNodes.map(async (fileNode) => {
const fileItem = getFileItem(fileNode.getId())
Expand All @@ -112,7 +114,17 @@ export class HeadlessSuperConverter implements SuperConverterServiceInterface {
} else {
this.exportEditor.update(
() => {
const fileExportNode = $createFileExportNode(fileItem.name, fileItem.mimeType)
filenameCounts[fileItem.name] =
filenameCounts[fileItem.name] == undefined ? 0 : filenameCounts[fileItem.name] + 1

let name = fileItem.name

if (filenameCounts[name] > 0) {
const { name: _name, ext } = parseFileName(name)
name = `${_name} - ${fileItem.uuid}.${ext}`
}

const fileExportNode = $createFileExportNode(name, fileItem.mimeType)
fileNode.replace(fileExportNode)
},
{ discrete: true },
Expand Down
10 changes: 9 additions & 1 deletion packages/web/src/javascripts/Utils/NoteExportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const noteRequiresFolder = (

const addEmbeddedFilesToFolder = async (application: WebApplication, note: SNNote, folder: ZipDirectoryEntry) => {
try {
const filenameCounts: Record<string, number> = {}
const embeddedFileIDs = headlessSuperConverter.getEmbeddedFileIDsFromSuperString(note.text)
for (const embeddedFileID of embeddedFileIDs) {
const fileItem = application.items.findItem<FileItem>(embeddedFileID)
Expand All @@ -166,7 +167,14 @@ const addEmbeddedFilesToFolder = async (application: WebApplication, note: SNNot
if (!embeddedFileBlob) {
continue
}
folder.addBlob(parseAndCreateZippableFileName(fileItem.title), embeddedFileBlob)
filenameCounts[fileItem.title] =
filenameCounts[fileItem.title] == undefined ? 0 : filenameCounts[fileItem.title] + 1
let name = fileItem.title
if (filenameCounts[fileItem.title] > 0) {
const { name: _name, ext } = parseFileName(fileItem.title)
name = `${_name} - ${fileItem.uuid}.${ext}`
}
folder.addBlob(parseAndCreateZippableFileName(name), embeddedFileBlob)
}
} catch (error) {
console.error(error)
Expand Down

0 comments on commit e455984

Please sign in to comment.