## 事象
[[Windows]]にて。
```ls
hoge
└── a
├── a.md
└── b
└── c
└── c.md
```
のような構成のとき、`c.md`で`[[a.md]]`を作成するとデッドリンクになる。
## 詳細
本来なら`/../../`のようになるべきところが`/..../`になっていた。
## 調査
`plugin.py`を調べてみた。
```python:plugin.py
# Walk through all files in docs directory to find a matching file
if filename:
for root, dirs, files in os.walk(self.base_docs_url):
for name in files:
# If we have a match, create the relative path from linker to the link
if self.simplify(name) == self.simplify(filename):
# if name == filename:
# Absolute path to the file we want to link to
abs_link_url = os.path.dirname(os.path.join(
root, name))
print("-------------abs_link_url----------")
print(abs_link_url)
print("-----------------------------------")
# Constructing relative path from the linker to the link
rel_link_url = os.path.join(
os.path.relpath(abs_link_url, abs_linker_url), name)
print("-------------rel_link_url----------")
print(rel_link_url)
print("-----------------------------------")
if title:
rel_link_url = rel_link_url + '#' + format_title
# 但这个在处理index.md标题或者是被用作子文件夹默认主页的标题
#会存在问题,因为这2种情况下网址格式跟普通的不一样
```
ポイントとなる部分のログは以下。
```console
-------------abs_link_url----------
C:\Users\tadashi-aikawa\git\github.com\tadashi-aikawa\docs-obsidian-various-complements-plugin\docs\1. Features
-----------------------------------
-------------rel_link_url----------
..\..\1. Features\Current file complement.md
-----------------------------------
-------------abs_link_url----------
C:\Users\tadashi-aikawa\git\github.com\tadashi-aikawa\docs-obsidian-various-complements-plugin\docs\resources
-----------------------------------
-------------rel_link_url----------
..\..\resources\current-file-complement-demo.gif
-----------------------------------
-------------abs_link_url----------
C:\Users\tadashi-aikawa\git\github.com\tadashi-aikawa\docs-obsidian-various-complements-plugin\docs\1. Features
-----------------------------------
-------------rel_link_url----------
..\..\1. Features\Current file complement.md
-----------------------------------
-------------abs_link_url----------
C:\Users\tadashi-aikawa\git\github.com\tadashi-aikawa\docs-obsidian-various-complements-plugin\docs\resources
-----------------------------------
-------------rel_link_url----------
..\..\resources\current-file-complement-off.gif
-----------------------------------
WARNING - Documentation file '2. Options\Current file complement\⚙️Enable Current file complement.md' contains a link to '2. Options\Current file complement\..46.\1. Features\Current
file complement.md' which is not found in the documentation files.
WARNING - Documentation file '2. Options\Current file complement\⚙️Enable Current file complement.md' contains a link to '2. Options\Current file
complement\..46.\resources\current-file-complement-demo.gif' which is not found in the documentation files.
WARNING - Documentation file '2. Options\Current file complement\⚙️Enable Current file complement.md' contains a link to '2. Options\Current file complement\..46.\1. Features\Current
file complement.md' which is not found in the documentation files.
WARNING - Documentation file '2. Options\Current file complement\⚙️Enable Current file complement.md' contains a link to '2. Options\Current file
complement\..46.\resources\current-file-complement-off.gif' which is not found in the documentation files.
```
- `rel_link_url`と`abs_link_url`に問題はなさそう
- 特に`rel_link_url`は正確
- `..\..`が`..46.`になっているのがポイント
- `\.`が`..46` ??
1階層でも問題は起こる。
```console
-------------rel_link_url----------
..\4. Terms\`Add a word to a custom dictionary` dialog.md
-----------------------------------
WARNING - Documentation file '3. Commands\🎮Add a word to a custom dictionary.md' contains a link to '4. Terms96Add a word to a custom dictionary` dialog.md' which is not found in the
documentation files.
```
`\`がいけない気がするんだが..
## 対策
`\`を強制的に`/`に置換するコードを入れたら動いた。
```diff:plugin.py
# Construct the return link
+ # Windows escapes "\" unintentionally, and it creates incorrect links, so need to replace with "/"
+ rel_link_url = rel_link_url.replace("\\", "/")
+
if filename:
if alias:
link = f'[{alias}]({rel_link_url})'
else:
link = f'[{filename+title}]({rel_link_url})'
```
ただし、バッククォートのように未対応の文字列は存在するので気を付ける必要がある。絵文字やスペースは問題なさそう。
## プルリク
> [!add] #2022/07/31 追記
> マージされてv0.1.4としてリリースされた。
以下に作成した。
<div class="link-card-v2">
<div class="link-card-v2-site">
<img class="link-card-v2-site-icon" src="https://github.githubassets.com/favicons/favicon.svg" />
<span class="link-card-v2-site-name">GitHub</span>
</div>
<div class="link-card-v2-title">
Windows escapes '\' unintentionally by tadashi-aikawa · Pull Request #2 · Jackiexiao/mkdocs-roamlinks-plugin
</div>
<div class="link-card-v2-content">
It unintentionally converts the link from ..\.. to ..46. on Windows.-------------abslinkurl----------C:\Users\t ...
</div>
<img class="link-card-v2-image" src="https://opengraph.githubassets.com/dea5c47aad9bb9d26e723ce1d0d76ad4b4d325a3867a7c1444a925204903fab8/Jackiexiao/mkdocs-roamlinks-plugin/pull/2" />
<a href="https://github.com/Jackiexiao/mkdocs-roamlinks-plugin/pull/2"></a>
</div>
本家にマージされるか不明だが、お困りの方はforkした以下のリポジトリからインストールしてもらえれば。
- https://github.com/tadashi-aikawa/mkdocs-roamlinks-plugin
```yaml
mkdocs-roamlinks-plugin = {git = "https://github.com/tadashi-aikawa/mkdocs-roamlinks-plugin.git"}
```
> [!add] #2025/02/22 追記
> #2022/07/30 にマージされていた 🎉