Skip to content

Commit

Permalink
fixed multiple media blocks with the same url export
Browse files Browse the repository at this point in the history
  • Loading branch information
iskaktoltay committed Oct 25, 2024
1 parent 0114b6f commit 298c93c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
6 changes: 4 additions & 2 deletions frontend/apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ ipcMain.on(
title: string
markdown: {
markdownContent: string
mediaFiles: {url: string; filename: string}[]
mediaFiles: {url: string; filename: string; placeholder: string}[]
}
}[],
) => {
Expand Down Expand Up @@ -214,9 +214,11 @@ ipcMain.on(
const uploadMediaFile = async ({
url,
filename,
placeholder,
}: {
url: string
filename: string
placeholder: string
}) => {
return new Promise<void>((resolve, reject) => {
const regex = /ipfs:\/\/(.+)/
Expand Down Expand Up @@ -252,7 +254,7 @@ ipcMain.on(
debug(`Media file successfully saved: ${mediaFilePath}`)
// Update the markdown content with the correct file name
updatedMarkdownContent = updatedMarkdownContent.replace(
filename,
placeholder,
filenameWithExt,
)
resolve()
Expand Down
4 changes: 2 additions & 2 deletions frontend/apps/desktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contextBridge.exposeInMainWorld('docExport', {
exportDocument: async (
title: string,
markdownContent: string,
mediaFiles: {url: string; filename: string}[],
mediaFiles: {url: string; filename: string; placeholder: string}[],
) => {
return new Promise((resolve, reject) => {
ipcRenderer.once('export-completed', (event, response) => {
Expand All @@ -70,7 +70,7 @@ contextBridge.exposeInMainWorld('docExport', {
title: string
markdown: {
markdownContent: string
mediaFiles: {url: string; filename: string}[]
mediaFiles: {url: string; filename: string; placeholder: string}[]
}
}[],
) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/apps/desktop/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function MainApp({
exportDocument={async (
title: string,
markdownContent: string,
mediaFiles: {url: string; filename: string}[],
mediaFiles: {url: string; filename: string; placeholder: string}[],
) => {
// @ts-ignore
return window.docExport.exportDocument(
Expand All @@ -257,7 +257,7 @@ function MainApp({
title: string
markdown: {
markdownContent: string
mediaFiles: {url: string; filename: string}[]
mediaFiles: {url: string; filename: string; placeholder: string}[]
}
}[],
) => {
Expand Down
6 changes: 4 additions & 2 deletions frontend/apps/desktop/src/save-markdown-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function saveMarkdownFile(
args: {
title: string
markdownContent: string
mediaFiles: {url: string; filename: string}[]
mediaFiles: {url: string; filename: string; placeholder: string}[]
},
) {
const {title, markdownContent, mediaFiles} = args
Expand Down Expand Up @@ -51,9 +51,11 @@ export async function saveMarkdownFile(
const uploadMediaFile = ({
url,
filename,
placeholder,
}: {
url: string
filename: string
placeholder: string
}) => {
return new Promise<void>((resolve, reject) => {
const regex = /ipfs:\/\/(.+)/
Expand Down Expand Up @@ -87,7 +89,7 @@ export async function saveMarkdownFile(
debug(`Media file successfully saved: ${mediaFilePath}`)
// Update the markdown content with the correct file name
updatedMarkdownContent = updatedMarkdownContent.replace(
filename,
placeholder,
filenameWithExt,
)
resolve()
Expand Down
8 changes: 4 additions & 4 deletions frontend/packages/app/app-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export type AppContext = {
exportDocument: (
title: string,
markdownContent: string,
mediaFiles: {url: string; filename: string}[],
mediaFiles: {url: string; filename: string; placeholder: string}[],
) => Promise<void>
exportDocuments: (
documents: {
title: string
markdown: {
markdownContent: string
mediaFiles: {url: string; filename: string}[]
mediaFiles: {url: string; filename: string; placeholder: string}[]
}
}[],
) => Promise<void>
Expand Down Expand Up @@ -64,14 +64,14 @@ export function AppContextProvider({
exportDocument: (
title: string,
markdownContent: string,
mediaFiles: {url: string; filename: string}[],
mediaFiles: {url: string; filename: string; placeholder: string}[],
) => Promise<void>
exportDocuments: (
documents: {
title: string
markdown: {
markdownContent: string
mediaFiles: {url: string; filename: string}[]
mediaFiles: {url: string; filename: string; placeholder: string}[]
}
}[],
) => Promise<void>
Expand Down
9 changes: 6 additions & 3 deletions frontend/packages/app/utils/blocks-to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ function convertBlocksToHtml(
}

async function extractMediaFiles(blocks: HMBlock[]) {
const mediaFiles: {url: string; filename: string}[] = []
const mediaFiles: {url: string; filename: string; placeholder: string}[] = []
let counter = 1
const extractMedia = async (block) => {
if (
block.type === 'image' ||
Expand All @@ -131,8 +132,10 @@ async function extractMediaFiles(blocks: HMBlock[]) {
return
}
const filename = url.split('/').pop()
mediaFiles.push({url, filename})
block.props = {...block.props, url: `media/${filename}`} // Update the URL to point to the local media folder
const placeholder = `file-${counter}`
mediaFiles.push({url, filename, placeholder})
counter++
block.props = {...block.props, url: `media/${placeholder}`} // Update the URL to point to the local media folder
}
}
if (block.children) {
Expand Down

0 comments on commit 298c93c

Please sign in to comment.