forked from PKM-er/Pkmer-Docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
571a603
commit 2053b73
Showing
3 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"formatType": "normal", | ||
"showRibbonIcon": true, | ||
"bindEnter": true, | ||
"bindTab": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
uid: 20240302213902 | ||
title: Wiki 链接和 markdown 链接之间的转换 | ||
tags: [正则表达式] | ||
description: 用VSCode+正则表达式实现转换 | ||
author: 西郊有密林 | ||
type: other | ||
draft: false | ||
editable: false | ||
modified: 20240314224554 | ||
--- | ||
|
||
# Wiki 链接和 markdown 链接之间的转换 | ||
|
||
## 分析 | ||
|
||
首先需要说明,我们想要的是什么样的效果: | ||
|
||
1. 文件链接:使用 wiki 链接,把 `[name](link)` 转化为 `[[link|name]]` | ||
2. 图片链接:使用 markdown 链接,把 `![[link|name]]` 转化为 `![name](link)` | ||
3. 外链:使用 markdown 链接 `[site](https://www.xxx.com/)` | ||
|
||
之所以要这样做,有几大理由: | ||
|
||
1. wiki 链接虽然为 Obsidian 独有,然而各家针对 Obsidian 已经做了很多配套,文件链接使用 markdown 链接往往被识别为附件或者外链(比如思源笔记、vercel); | ||
2. Obsidian 的外链只能使用 markdown 链接,使用 wiki 链接会无法跳转 | ||
|
||
## 用 VSCode 实现转换 | ||
|
||
> [!WARNING] | ||
> 本文使用的 VSCode 内的正则表达式替换,不要搞错别的地方的了。 | ||
按下 `Ctrl+Shift+H` 来到正则替换界面,记得打开正则匹配以及区分大小写: | ||
|
||
![Pasted image 20240302214116](https://cdn.pkmer.cn/images/202403142246024.png!pkmer) | ||
|
||
笔记库中的文件链接如果采取 markdown 链接的话,可以这样匹配: | ||
|
||
``` | ||
\[([^\^]+?)\]\((.+?)\) | ||
``` | ||
|
||
然后将其替换为: | ||
|
||
``` | ||
[[$2|$1]] | ||
``` | ||
|
||
一般人不会给图片取别名,所以这个表达式不会匹配到图片链接。但是这样做会将 markdown 格式的外链也转化为 wiki 格式,Obsidian 是不认的,因此还需要把外链专门转化一下。匹配被转化的外链: | ||
|
||
``` | ||
\[\[(http[s]?://.+)\|(.*?)\]\] | ||
``` | ||
|
||
转化回来: | ||
|
||
``` | ||
[$2]($1) | ||
``` | ||
|
||
如果确实有给图片取别名的习惯,则还需要转化一次。匹配: | ||
|
||
``` | ||
!\[\[(.+?jpg|png|gif|svg)\|(.*)\]\] | ||
``` | ||
|
||
转换: | ||
|
||
``` | ||
![$2]($1) | ||
``` | ||
|
||
> [!WARNING] | ||
> 我不能保证这个正则替换方法考虑到了所有链接的情况,可能在处理某些链接下还是会有瑕疵。因此我建议在用 VSCode 替换的时候多看两眼并且做好备份,避免开弓没有回头路。 | ||
## 关于 markdown 兼容性的问题 | ||
|
||
使用 markdown 链接可以保证兼容性,这是许多人坚持用 markdown 风格链接的理由。然而 markdown 标准带来的兼容性可能已经在事实上消失了。 | ||
|
||
markdown 标准作为纯文本时代的产物,如今在笔记领域爆火有其适应了社会发展的一面。但是由于 markdown 标准长期缺乏维护,数十年以来几乎没有发展,这导致了各家编辑器都开始制定自己的标准。在如今,我们最常用的一些笔记元素,比如数学公式、链接、表格、脚注,这些都是不被包含在标准 markdown 中的。实际上,尽管都兼容 markdown 标准,但是笔记库已经越来越难以迁移了。 | ||
|
||
考虑一个有 1000 条笔记的库,现在你对它进行了迁移,结果发现所有的块引用都失效了。那么现在可能就有上千个块引用分散在 1000 个文件中,这需要手动修改。虽然似乎只是迁移出现了一点瑕疵, 90% 的内容迁移都没出问题。然而只是这 10% 的瑕疵,就产生了完全不可接受的工作量。 | ||
|
||
各家大同小异的 markdown 方言迁移过去实际上就造成了许多个这样的瑕疵。 | ||
|
||
- typora 的公式换行在 Obsidian 中无效 | ||
- Obsidian 的公式矩阵左对齐在思源中无效 | ||
- Obsidian 的表格在渲染模式中必须前后空行,编辑模式中就不需要 | ||
- Obsidian 的脚注在思源中无效 | ||
- Obsidian 的 markdown 风格文件链接在思源中被识别为附件 | ||
- Obsidian 的 markdown 风格文件链接在 Vercel 中被识别为外链 | ||
- 语雀导出的 markdown 文件有众多莫名其妙的换行符 | ||
- …… | ||
|
||
所以,与其严守 markdown 标准使用 markdown 风格的链接去追求兼容性,不如选择一个能保证若干年后也能像现在这样写笔记的软件,然后支持它去拓展新的功能。这就是为什么我会提倡开头介绍的链接风格。 |