platers / obsidian-linter

An Obsidian plugin that formats and styles your notes with a focus on configurability and extensibility.
https://platers.github.io/obsidian-linter/
MIT License
1.26k stars 83 forks source link

FR: Add options to convert wiki-links to markdown-links and convert markdown links to wiki-links #428

Open aiyolo opened 2 years ago

aiyolo commented 2 years ago

Is Your Feature Request Related to a Problem? Please Describe.

images with wiki links are not shown when notes are opened in other editors.

Describe the Solution You'd Like

Add options to convert wiki-links to markdown-links and convert markdown links to wiki-links

Please include an example where applicable:

original
[[sample.jpg]]
expected
[](sample.jpg)
[](relative path of sample.jpg)
[](absolute path of samplle.jpg)

wise versa

pjkaufman commented 2 years ago

Thanks for the feature request! Do you mind clarifying if you mean all wiki links and all markdown links or just images? The examples provided are regular links to images, but not actual image links in markdow, so I want to make sure I am understanding the request correctly.

aiyolo commented 2 years ago

I mean all markdown links and wiki links. The format of wiki links is [[]], while that of markdown links is [](). Image is only one file format that used to link. I expect I can freely switch between [[]] and []()

aiyolo commented 2 years ago

just like obsidian-link-converter

orca131 commented 1 year ago

I would like to update this FR because links conversion is still important.

Please, pay attention for the heading links work.

  1. When anchor (heading) is located on the other page, a link, header name should be used as link text.
    • link before conversion [[note#header]]
    • expected result [header](note.md#header)
  2. When anchor (heading) is located on the same page as a link, header name should be used as link text and not include # symbol.
    • link before conversion [[#header name]]
    • expected result [header name](#header%20name)
  3. The wiki-link alias should be used as text of link. URI shouldn't include | symbol or alias text.
    • link before conversion [[note#header|alt-text]]
    • expected result [alt-text](note.md#header)
  4. And of course, links inside the code block must be ignored.
    • link before conversion `[[note#header]]`
    • expected result `[[note#header]]`

This is important because other plugins such as obsidian-link-converter do not work properly with this.

pjkaufman commented 1 year ago

It looks to me like there is still a desire to keep this feature request open, so I am going to reopen this FR.

pjkaufman commented 1 year ago

I am taking a look at this, but it looks like if I go this route, I will be reliant on the Obsidian API to make this work. It is not out of the question, but I will need to work on things in order to make it play nicer with the tests to make sure it works and is reliable ahead of time.

pjkaufman commented 1 year ago

Somewhat relates to #780 .

pjkaufman commented 1 year ago

I will note that I started work on this, but there is a lot to tackle, so I will need to find a decent time slot to fit this into. I do not plan on copying the logic as it is from the other plugin so I will be writing my own version of the functionality. It may start out having less features and work its way up. As of right now, I am still in the preliminary phases when it comes to working on this.

aiyolo commented 1 year ago

Looking forward to see the next version.

pjkaufman commented 1 year ago

Looking forward to see the next version.

I am not sure this will make the next release given its level of difficulty. So I would not get your hopes up in that regard. But I do plan to get this done. I just think we are talking something like 20+ dev hours to get there (though I could be wrong).

pjkaufman commented 1 year ago

Without me diving too far into things, how does the referenced plugin handle going from wiki links to markdown links for images when there is a width specified?

I am planning on just losing that info or adding a setting to allow it to be lost where not selecting it will generate a warning in the logs and leaving those links alone.

Vinfall commented 1 year ago

For the plugin way of handling this, I think it's located at https://github.com/ozntel/obsidian-link-converter/blob/b2973dcecbac79171876b9f55edb242519136a7f/src/converter.ts#L207-L226

For images when people specify width, I think images are usually used in preview like this:

![[sample.png|400]]
![Alt text|100x200](https://example.com/some-external-image.webp)

So if images are not used in preview (aka. not ! before the link format), just treat them like links.

pjkaufman commented 1 year ago

@Vinfall , I don't believe ![Alt text|100x200](https://example.com/some-external-image.webp) is valid markdown as default markdown does not have image size.

Vinfall commented 1 year ago

You are right, raw markdown/GFM do not recognize that. But Obsidian does allow the formatting (and works just like ![[sample.png|400]]) and given the scope of our discussion it makes sense to me.

Either way, that width info is discarded when converting from Markdown link to wiki link (if wiki link doesn't have similar workaround). My point is you should consider previewed/linked images differently.

pjkaufman commented 1 year ago

@Vinfall , are you saying that images are not to be including in this rule? I am asking since I have no way to differentiate between wiki links and wiki images until I have a list of matches for the regex as it stands.

Edit: Note that an image is a link

Vinfall commented 1 year ago

It seems I misuse the term of wiki link in previous comments, sorry for the confusion. I hope I understand your question correctly this time.

TLDR: see chart.

have no way to differentiate between wiki links and wiki images

If you mean external images, these are links of course but they always start with !. In Obsidian wikilinks are always [[links]] and ![[images]], you can check this in the settings. A linked image like [[sample.png]] is a link but not an image in the sense of wikilink.

I understand your confusion of distinguish linked content like [[sample.png|alt text]] and [[another note|alt text]]. But they are converted into [alt text](sample.png) and [alt text](another note) in the same way.

Width is only intended to be used in images like ![[sample.png|300]] and ![300](sample.png) (yes, this works to resize the image and 300 is not alt text), not links like [[sample.png|300]].

If users misuse the feature and do formatting like [[sample.png|300]], just discard width info. I think this can be done by detecting file extension (png, .jpg etc.) and ! symbol. Alternatively, you can distinguish this by checking the alt text (a number, something like 200x100 or string, although technically they are all string, which should be harder).

...images are not to be included

Check the following chart, what you asked should be Case D. In that case, discard width info and convert without alt text.

Case Input Format Output
A [[sample.png\|alt text]] Wiki link, link [alt text](sample.png)
B [[another note\|alt text]] Wiki link, link [alt text](another note)
C ![[sample.png\|300]] Wiki link, image ![300](sample.png)
D [[sample.png\|300]] Wiki link, link [](sample.png)
pjkaufman commented 1 year ago

@Vinfall , thanks for that run down.

I guess I need to clarify exactly what is wanted. Is the desire just for converting from a markdown link/image to a wiki link/image and vice versa or is there some level of customization of how that works that is being requested? Because if it it is just converting one to the other, that is likely much easier than allowing customization of how the links appear (relative, absolute, as is, etc.).

Vinfall commented 1 year ago

Initial support for converting between markdown/wiki links as is should suffice, just like orca131 commented.

I don't need customization personally but it can surely be added upon user request after the feature is added.

pjkaufman commented 1 year ago
Alright. I believe I have something that will work for the simple cases of converting markdown links to wiki links. It will work like the following: Scenario Input Link Output Link
Regular markdown link [link text](file.md) [[file\|link text]]
Regular markdown link with path [link text](nested-file/file.md) [[nested-file/file.md\|link text]]
Markdown link missing display text [](file.md) [[file]]
Markdown link with header and link text [header](file.md#header) [[file#header\|header]]
Markdown link with header and without link text [](file.md#header) [[file#header]]
Markdown link with paragraph reference and link text [link text](file.md#^0b927e) [[file#^0b927e\|link text]]
Markdown link with paragraph reference and without link text [](file.md#^0b927e) [[file#^0b927e]

Images and URLs would be ignored. Images may get covered later. Do note that there is no file path truncation when converting to wiki links in this implementation. You get exactly what the markdown link specifies. If there is a desire to do some kind of truncation for the file path, that is possible, but it relies on obsidian so it would not be as testable (i.e. it would require live testing to ensure it does not break). It is feasible to do this if we want to go that route.

I am now starting work on the reverse converter which has no choice but to rely solely on obsidian logic since there is no other way to properly convert from wiki links to markdown links.

pjkaufman commented 12 months ago

I wanted to give an update on this. I have had to put any changes around this on hold as I am prioritizing getting a couple of other things done before I come back to this. I would like to get the Linter off of the main thread in Obsidian to avoid it blocking up the UI to help fix some issues that have been encountered and provide a better experience, and also get caching of what was linted when and what the contents were to allow for a better experience as well. This would open up opportunities to fix up several areas that are currently having problems in the Linter.

In the meantime, I am open to a PR adding this feature or even adding a base to properly work with moving forward while I am working on these other areas. I hope y'all have a blessed day!