Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local images from file system should resolve to Iris/Images as the default path to look in #43

Open
alexwkleung opened this issue Sep 21, 2023 · 7 comments · May be fixed by #90
Open
Assignees
Labels
enhancement New feature or request general General discussion of a specific topic

Comments

@alexwkleung
Copy link
Owner

If a user wants to insert a local image from the file system, the default path should resolve to Iris/Images.

The folder helps keep all images in one place and makes it easy to backup the Iris directory. This should avoid path issues for local images at least.

Drag and drop images is another feature that would be good to have. However, that will be in a future release at the earliest.

@alexwkleung alexwkleung added enhancement New feature or request general General discussion of a specific topic labels Sep 21, 2023
@alexwkleung alexwkleung self-assigned this Sep 21, 2023
@alexwkleung
Copy link
Owner Author

alexwkleung commented Sep 22, 2023

I quickly tested this Electron protocol, and it seems to work fine. This is apparently a safe way of doing it, since many older methods forcefully disable flags and whatnot in the Electron main process, causing unwanted security issues.

Reference: https://www.electronjs.org/docs/latest/api/protocol

  1. In index.ts
import { protocol, net } from 'electron'

//initialize app

//custom protocol to handle local file system absolute paths
app.whenReady().then(() => {
  protocol.handle('image', (request) =>
    net.fetch('file://' + request.url.slice('image://'.length)))
})
  1. Add image: data: 'self' to CSP in index.html

  2. In markdown-parser.ts, set ALLOW_UNKNOWN_PROTOCOLS to true in DOMPurify options. Not sure how safe this is, but for now it's fine.

  3. Launch Iris in dev

  4. Change to Advanced Mode

  5. Use image syntax with the custom protocol to search the image locally

![test](image::///Users/alex/Iris/Images/test.png)
  1. Change to Reading Mode, and you should see the image loaded from the local filesystem.

For now, it doesn't have to reside within the main notes folder, but in future releases, there will be an Images folder within the Iris directory. It would be logical to force the resolve path so users won't have issues with loading their images in case they accidentally delete them.

@alexwkleung
Copy link
Owner Author

alexwkleung commented Sep 22, 2023

I quickly tested this Electron protocol, and it seems to work fine. This is apparently a safe way of doing it, since many older methods forcefully disable flags and whatnot in the Electron main process, causing unwanted security issues.

Reference: https://www.electronjs.org/docs/latest/api/protocol

  1. In index.ts
import { protocol, net } from 'electron'

//initialize app

//custom protocol to handle local file system absolute paths
app.whenReady().then(() => {
  protocol.handle('image', (request) =>
    net.fetch('file://' + request.url.slice('image://'.length)))
})
  1. Add image: data: 'self' to CSP in index.html
  2. In markdown-parser.ts, set ALLOW_UNKNOWN_PROTOCOLS to true in DOMPurify options. Not sure how safe this is, but for now it's fine.
  3. Launch Iris in dev
  4. Change to Advanced Mode
  5. Use image syntax with the custom protocol to search the image locally
![test](image::///Users/alex/Iris/Images/test.png)
  1. Change to Reading Mode, and you should see the image loaded from the local filesystem.

For now, it doesn't have to reside within the main notes folder, but in future releases, there will be an Images folder within the Iris directory. It would be logical to force the resolve path so users won't have issues with loading their images in case they accidentally delete them.

The protocol above allows advanced mode users to use the local filesystem as a path for greater flexibility. This will be part of the next update for Iris.

Note for Basic Mode users:

You can copy an image and paste it directly into the editor. It will transform the pasted image data into base64, creating a hard link. I don't remember what package or lines of code that enables this, so I'll take a look at it when I have time.

@alexwkleung
Copy link
Owner Author

Since this feature doesn't affect the critical parts the core (only changes to Electron main process and Markdown parser), I might create a commit for this in the main branch within the next week or so. If that's the case, the update will be v0.2.0-dev-4.3.

This is a very simple addition to the code and can be easily removed or modified if necessary.

@alexwkleung
Copy link
Owner Author

 protocol.handle('image', (request): Promise<Response> => {
    return net.fetch('file://' + request.url.slice('image://'.length)).catch((e) => console.error(e)) as Promise<Response>
 })

Not 100% necessary but just added types and have it able to throw exceptions if needed.

@alexwkleung
Copy link
Owner Author

Changes are live in v0.2.0-dev-4.3 branch.

I won't merge into main branch yet since I want to tweak/add a few things.

@alexwkleung
Copy link
Owner Author

Changes are live in v0.2.0-dev-4.3 branch.

I won't merge into main branch yet since I want to tweak/add a few things.

I will submit a PR instead of directly merging into main once v0.2.0-dev-4.3 is ready. The PR makes it easier for tracking and future reference.

@alexwkleung alexwkleung linked a pull request Dec 28, 2023 that will close this issue
@alexwkleung
Copy link
Owner Author

Reference from #90:

In ed77dc9, I added a default path to the local protocol so that it includes the home directory + /Iris/Images.

The syntax to link images from Iris/Images will be in the form ![alt_name](local://image_name). Make sure to add the file extension as well.

Will be closed once v0.3.0-dev.1 releases. This is complete as of now.

@alexwkleung alexwkleung removed a link to a pull request May 31, 2024
@alexwkleung alexwkleung linked a pull request May 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request general General discussion of a specific topic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant