备注-wiki-link
remark 解析和渲染wiki风格链接的插件 [[...]] 财务报告和审定财务报表 ![[...]] 在马克下。 这不管你有多厉害 财务报告和审定财务报表 它特别是为了支持"Obsidian"风格链接.
支持的特性
- 支助
[[Internal link]] - 支助
[[Internal link|With custom text]] - 支助
[[Internal link#heading]] - 支助
[[Internal link#heading|With custom text]] - 支助
![[Image.png]](带有可选维度的图像:![[image.jpg|200x300]](中文(简体) ) - 支助
![[Video.mp4]](带有可选维度的视频:![[video.mp4|640x480]](中文(简体) ) - 支助
![[Audio.mp3]] - 支助
![[Document.pdf]]
未来支助:
- 支助
![[Embed note]] - 支助
![[Embed note#heading]]
安装
npm install @flowershow/remark-wiki-link
使用量
import { unified } from "unified";
import remarkParse from "remark-parse";
import wikiLinkPlugin from "@flowershow/remark-wiki-link";
const processor = unified().use(remarkParse).use(wikiLinkPlugin);
配置选项
format
类型 : "regular" | "shortestPossible"
默认 : "shortestPossible"
维基链接目标与文件的匹配控制 :
"regular": 精确路径匹配 。 维基链接目标必须精确匹配文件路径(不包括扩展).- 示例
[[blog/my-post]]只会匹配blog/my-post.md
- 示例
"shortestPossible": Obsidian-风格相配. 维基链接目标与文件路径的结尾相匹配,并使用最短的匹配路径.- 示例
[[my-post]]可以匹配两者blog/my-post.md财务报告和审定财务报表docs/my-post.md,但将决心 任何有短路径 - 您可以使用短维基链接, 如
[[my-post]]而不是完全的路径像[[blog/my-post]]
- 示例
files
类型 : Array<string>
默认 : []
用于匹配wikilinks的文件路径列表. 支持两者 .md 财务报告和审定财务报表 .mdx 文档。 没有匹配文件的 Wikilink 将拥有 new 课.
(当使用时) format: "shortestPossible",此列表用于解决被缩短的路径到其完整路径. )
permalinks
类型 : Record<string, string>
默认 : {}
文件路径图到它们自定义的永久链接(例如. { "path/to/file.md": "/custom-permalink" }) (中文(简体) ). 当一个wikilink匹配一个已定义了 permalink 的文件时, 将使用 permalink 代替 urlResolver。这对 Obsidian 风格的 permalink 支持有用,因为您需要特定文件的 URL .
示例
const processor = unified()
.use(remarkParse)
.use(wikiLinkPlugin, {
files: ["blog/my-post.md", "docs/guide.md"],
permalinks: {
"blog/my-post.md": "/blog/custom-url",
"docs/guide.md": "/documentation/guide",
},
});
使用此配置 :
[[blog/my-post]]将链接到/blog/custom-url[[blog/my-post#heading]]将链接到/blog/custom-url#heading[[docs/guide]]将链接到/documentation/guide
className
类型 : string
默认 : "internal"
类名添加到所有wiki链接并嵌入了节点.
newClassName
类型 : string
默认 : "new"
分类名称添加到没有匹配的 permalink 的节点( 已通过 ) permalinks 选项).
urlResolver
类型 : (name: string) => string
默认 : (name: string) => name
一个能解决一个维基链接(或嵌入)目标到URL路径的函数. 目标在 [[target|alias]] 或者说 ![[target]]。 。 。 .
aliasDivider
类型 : string
默认 : "|"
在解析和字符串化期间,用于将链接目标与其在维基链接中的别名相分离的字符. 例如,在 [[target|alias]],分词是 |。 。 。 .
此选项允许您使用不同的维基链接语法. 例如,你可以使用 : 设置为分隔符 aliasDivider: ":",将链接解析和字符串为 [[target:alias]]。 。 。 .
生成文件列表
如果您在维基链接中使用了尽可能短的路径格式,则需要设置 option.format: "shortestPossible" ,并向插件提供您内容文件夹中的文件列表 option.files您可以使用自己的脚本生成此列表,也可以使用类似文件系统工具 glob数字 :
import { unified } from "unified";
import remarkParse from "remark-parse";
import wikiLinkPlugin from "@flowershow/remark-wiki-link";
import glob from "glob";
// Get all markdown files (both .md and .mdx)
const files = glob.sync("**/*.{md,mdx}", { cwd: "content" });
const processor = unified().use(remarkParse).use(wikiLinkPlugin, {
format: "shortestPossible",
files,
});
与 Obsidian 永久链接使用
如果您正在使用 Obsidian 并想要将特定文件映射到自定义 URL( permarinks), 您可以使用 permalinks 选项 :
import { unified } from "unified";
import remarkParse from "remark-parse";
import wikiLinkPlugin from "@flowershow/remark-wiki-link";
import glob from "glob";
// Get all markdown files (both .md and .mdx)
const files = glob.sync("**/*.{md,mdx}", { cwd: "content" });
// Create a map of file paths to their custom permalinks
const permalinks = {
"blog/2024/my-post.md": "/blog/my-custom-url",
"docs/getting-started.md": "/docs/start",
// ... more mappings
};
const processor = unified().use(remarkParse).use(wikiLinkPlugin, {
format: "shortestPossible",
files,
permalinks,
});
正在测试
npm test