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

Index []() style links #827

Merged
merged 16 commits into from May 27, 2024
Merged

Index []() style links #827

merged 16 commits into from May 27, 2024

Conversation

onespaceman
Copy link
Contributor

  • style links
    • No longer opens in new window if link is local
    • Now indexed if local
    • Page completion
    • Changed on rename and refactor
      • Refactor now uses link position from the index to change references
  • New query for linked attachments

@zefhemel
Copy link
Collaborator

This is very nice! Let me have a closer look at this and do some testing.

@zefhemel
Copy link
Collaborator

I've noticed that linking to other pages with the [hello](somepage) only works for pages linked to without spaces, so [hello](some page) doesn't work, because the underlying markdown parser doesn't accept it. I was thinking of different options, but probably the best one would be to patch the markdown parser somehow. Alternatively we just don't support page links with spaces, or support them, but URL encoded (e.g. other%20page) but that doesn't seem great either.

@zefhemel
Copy link
Collaborator

Another question is: should attachment links be tagged with attachment or e.g. attachmentlink or attachmentLink. I think it would be useful to reserve attachment (the files themselves) and index them with their own attributes (such as file size, lastModified, contentType etc.)

plugs/index/refactor.ts Outdated Show resolved Hide resolved
@onespaceman
Copy link
Contributor Author

I've noticed that linking to other pages with the [hello](somepage) only works for pages linked to without spaces, so [hello](some page) doesn't work, because the underlying markdown parser doesn't accept it. I was thinking of different options, but probably the best one would be to patch the markdown parser somehow. Alternatively we just don't support page links with spaces, or support them, but URL encoded (e.g. other%20page) but that doesn't seem great either.

Yeah, apparently the correct way to enclose the link with <> if it has whitespaces. It doesn't work right now, but I could add it and call it a day. I'm not a fan, but if the spec says this is how it must be, who am I to argue?

@onespaceman
Copy link
Contributor Author

Another question is: should attachment links be tagged with attachment or e.g. attachmentlink or attachmentLink. I think it would be useful to reserve attachment (the files themselves) and index them with their own attributes (such as file size, lastModified, contentType etc.)

Good point. another option is to combine with regular LinkObjects as a union

type LinkObject = ObjectValue<{
  // Page Link
  // The page the link points to
  toPage: string;
  // The page the link occurs in
  page: string;
  pos: number;
  snippet: string;
  alias?: string;
  asTemplate: boolean;
  toFile?: never;
} | {
  // Attachment Link
  // The file the link points to
  toFile: string;
  // The page the link occurs in
  page: string;
  pos: number;
  snippet: string;
  toPage?: never;
  alias?: never;
  asTemplate?: never;
}>;

What do you think?

@zefhemel
Copy link
Collaborator

I've noticed that linking to other pages with the [hello](somepage) only works for pages linked to without spaces, so [hello](some page) doesn't work, because the underlying markdown parser doesn't accept it. I was thinking of different options, but probably the best one would be to patch the markdown parser somehow. Alternatively we just don't support page links with spaces, or support them, but URL encoded (e.g. other%20page) but that doesn't seem great either.

Yeah, apparently the correct way to enclose the link with <> if it has whitespaces. It doesn't work right now, but I could add it and call it a day. I'm not a fan, but if the spec says this is how it must be, who am I to argue?

Really? First time I heard of that notation. Markdown is a new surprise every day 😆

@zefhemel
Copy link
Collaborator

Another question is: should attachment links be tagged with attachment or e.g. attachmentlink or attachmentLink. I think it would be useful to reserve attachment (the files themselves) and index them with their own attributes (such as file size, lastModified, contentType etc.)

Good point. another option is to combine with regular LinkObjects as a union

type LinkObject = ObjectValue<{
  // Page Link
  // The page the link points to
  toPage: string;
  // The page the link occurs in
  page: string;
  pos: number;
  snippet: string;
  alias?: string;
  asTemplate: boolean;
  toFile?: never;
} | {
  // Attachment Link
  // The file the link points to
  toFile: string;
  // The page the link occurs in
  page: string;
  pos: number;
  snippet: string;
  toPage?: never;
  alias?: never;
  asTemplate?: never;
}>;

What do you think?

Actually yeah, that makes sense, let's do that. I suppose you can easily filter one or the other by filtering on either toPage or toFile.

@onespaceman
Copy link
Contributor Author

onespaceman commented Mar 27, 2024

While I'm doing this I'm also adding in updating attachment links on rename/refactor (fixes #733)
questions:

  1. Should attachment links be changed to be the same as wikilinks where every link assumes it's from the base folder, or should it refer to the note's folder like it is now? ie Page is [[subfolder/Page]]. image.png is inside subfolder. Link right now -> [](image.png). Should it be [](subfolder/image.png)?
  2. Should I add support for attachment wikilinks? -> ![[image.png]]
  • Should it be the default?
  • If added then implementing the change in question 1 makes sense
  1. When a Page is moved where do the attachments go?
  • Option 1: Only update the links, Keep the attachment where it is.
  • Option 2: If the attachment is in the same folder as the page, and is only linked to on that page, move it with the page. Otherwise do option 1

@zefhemel
Copy link
Collaborator

zefhemel commented Apr 4, 2024

As for 1. Right, so the semantics of links and image links should be that they’re relative, to be consistent with default markdown behavior.

As for 2. I think that would be great actually. And making that the default drag & drop etc. behavior… good question. Maybe?

As for 4. Option 2 would be the nicest I’d say

@onespaceman
Copy link
Contributor Author

Ok, this turned out to be pretty big, so I did a hard reset and tried to break it into digestible commits.
I tried to be as thorough as possible with testing because it involves editing/moving pages so I don't want to mess up anyone's files.

Here is a basic space setup I used for testing how renaming works with rewriting links
https://gist.github.com/onespaceman/da4eaed2e6808e7342840c9f680ce84d
I also used a copy of my personal vault to rename and prefix rename everything and anything, it seems to work perfectly, but please take your time to test it out yourself.

Also I had a really weird bug with importing regex constants.
https://github.com/onespaceman/silverbullet/blob/b17a2d0d468034d5434a88c7102639bf0a4da3d2/web/cm_plugins/inline_image.ts#L67
https://github.com/onespaceman/silverbullet/blob/b17a2d0d468034d5434a88c7102639bf0a4da3d2/web/cm_plugins/inline_image.ts#L70
When using the imported regex, the inline images would only show up every other image, and the visible images would disappear and the not shown images would appear on any refresh, such as moving the cursor or typing a character. This doesn't happen when I would replace the constant by pasting the same regex directly. Idk what is going on there.

@zefhemel
Copy link
Collaborator

Ok let me give this a try. We clearly need an end-to-end style testing framework for SB for things like this 😆

Copy link
Collaborator

@zefhemel zefhemel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all: thanks so much for doing this work. The PR is pretty large and it's pretty hard to review, so have a look at the few things I mentioned and at the same time I'll do a local checkout and play with it to see if I can make it break.

plug-api/lib/resolve.ts Show resolved Hide resolved
plugs/index/refactor.ts Outdated Show resolved Hide resolved
@zefhemel
Copy link
Collaborator

zefhemel commented May 14, 2024

Issue:

When auto completing an image URL with a space in the name, it expands to a syntax with <, e.g.

![bla](</zefzefzef 3/americans-europeans-2.jpeg>)

which then breaks in the live preview (it shows the image as a broken link). I think this may just not be valid markdown at all, and may have to resort to a URL encoded URL?

Update: funny thing is that with links to wiki pages this does work:

[efef](<test attachment page>)

clicking that link works fine.

common/common_system.ts Outdated Show resolved Hide resolved
plugs/index/attachment.ts Outdated Show resolved Hide resolved
@zefhemel
Copy link
Collaborator

Overall, once again: thanks for making this effort. It's looking really good, and we now will have code complete for attachments, which I'm pretty sure was missing until now. At some point SB may actually become decent for stuff that involves not primarily text files 😆

@onespaceman
Copy link
Contributor Author

I think that covers those issues. let me know if anything else needs changing

@zefhemel
Copy link
Collaborator

This looks almost good to go, just found one issue:

You auto complete a page with [[something it now completes attachment files too, not just pages. Now the question is actually: while a regression, is this perhaps desired? That way you can link to attachments without using []() style links 🤔 Did you do this on purpose?

@zefhemel
Copy link
Collaborator

zefhemel commented May 19, 2024

Ok, it is inconsistent with the ![[image.png]] syntax that is also supported now (I even forgot we have this, or did you add it here?) at least when completing ![[image.png I'd expect only attachments (perhaps even just images) to be completed, not pages as well.

To be honest, I'd prefer to keep using [[page]] just for pages, and ![[image.png]] for attached images only (until we make that syntax also support pages and effectively be transclosures: #533).

@zefhemel
Copy link
Collaborator

Ok I think you indeed added the ![[image.png]] in this PR as well correct? That's cool, but then I found an additional bug:

  • When you have a page in a folder and drag & drop an image into, it translates this to ![[image.png]] which breaks, because it assumes an absolute path (so should include the current folder), so the folder name should be prepended to it. This is different than the previous behavior of using the ![]() syntax, where paths are relative.

@onespaceman
Copy link
Contributor Author

That should fix the upload path. I'll fix merge conflicts later.

@onespaceman
Copy link
Contributor Author

Embeding page links is the next thing I'm working on. So if you don't mind it doing nothing for a little bit, I'll just leave it as is.
I was just going to make it a shortcut for a live template.
Making it an editable transclosure is probably a future pr (idk if I even have the knowledge to do it)

@zefhemel
Copy link
Collaborator

Sure, that sounds great. Making the tansclosure editable is not a priority imo. Using it as a live template is also what I'd go for.

@onespaceman
Copy link
Contributor Author

#833 is unfortunately a major pain for refactoring and for autofilling links.
Do you mind if I rewrite it to use ![alt text | resolution](filename.png) format suggested in #493 . It would also match the format for wikilinks ![[image.png|resolution]]

@onespaceman
Copy link
Contributor Author

pinging @fflorent for input as well

@zefhemel
Copy link
Collaborator

Personally I have no strong preference. I don't know to what extent this is any way standardized. Incompatibility with other tools (maybe GitHub not sure of it even supports it) would be by only concern.

@onespaceman
Copy link
Contributor Author

It's not standardized at all. Obsidian uses the format I suggested, most of the rest, including github use html embeds.
<img src="image.png" width="200" height="100">

This way is probably better because links will still work as normal

@zefhemel
Copy link
Collaborator

I'm perfectly fine with changing it then

@onespaceman
Copy link
Contributor Author

there. should be ready

@zefhemel
Copy link
Collaborator

Ok this looks good and seems to work as expected

Two questions remain:

  • Auto completing ![[ shows also pages, is this in preparation for the "embedding" that you'll implement later? If so, then maybe it's ok to leave it like this for a bit
  • Auto completing [[ also auto completes attachments, is this intended? I can see arguments for it

@onespaceman
Copy link
Contributor Author

yes to both.
For 2, I'm thinking that wikilinks = local files

@zefhemel
Copy link
Collaborator

Alright. Let's do this!

@zefhemel zefhemel merged commit 1f94915 into silverbulletmd:main May 27, 2024
1 check passed
@zefhemel
Copy link
Collaborator

Thanks again. This is awesome stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants