Skip to content

Commit cec85c1

Browse files
committed
feat(bi-directional-links): support to render inline tokens
1 parent fd45582 commit cec85c1

3 files changed

Lines changed: 84 additions & 3 deletions

File tree

  • docs/pages
  • packages/markdown-it-bi-directional-links/src

docs/pages/en/integrations/markdown-it-bi-directional-links/index.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,34 @@ Demo
8989

9090
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|Custom Text]]
9191

92+
### Use Markdown markup in custom text
93+
94+
```markdown
95+
[[Some Full Path Reference to Your Page|`Code Block (Before)` Middle `Code Block (After)`]]
96+
97+
[[Some Full Path Reference to Your Page|**Bold Before** Middle **Bold After**]]
98+
99+
[[Some Full Path Reference to Your Page|*Italic Before* Middle *After*]]
100+
101+
[[Some Full Path Reference to Your Page|~~Strikethrough Before~~ Middle ~~Strikethrough After~~]]
102+
103+
[[Some Full Path Reference to Your Page|<span style="color: red;">Custom HTML</span>]]
104+
105+
[[Some Full Path Reference to Your Page|<span style="color: red;">Custom HTML (Before)</span> Middle <span style="color: blue;">Custom HTML (After)</span>]]
106+
```
107+
108+
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|`Code Block (Before)` Middle `Code Block (After)`]]
109+
110+
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|**Bold (Before)** Middle **Bold (After)**]]
111+
112+
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|*Italic (Before)* Middle *Italic (After)*]]
113+
114+
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|~~Strikethrough (Before)~~ Middle ~~Strikethrough (After)~~]]
115+
116+
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|<span style="color: red;">Custom HTML</span>]]
117+
118+
[[pages/en/integrations/markdown-it-bi-directional-links/Bi-directional Links Example Page|<span style="color: red;">Custom HTML</span> Middle <span style="color: blue;">Custom HTML</span>]]
119+
92120
## How to use
93121

94122
### Installation

docs/pages/zh-CN/integrations/markdown-it-bi-directional-links/index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ Obsidian 同名页面:[[pages/zh-CN/integrations/markdown-it-bi-directional-li
8989

9090
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|自定义文案]]
9191

92+
#### 自定义文案中使用 Markdown 语法
93+
94+
```markdown
95+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|`代码块(前缀)` 中间的内容 `代码块(后缀)`]]
96+
97+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|**粗体(前缀)** 中间的内容 **粗体(后缀)**]]
98+
99+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|*斜体(前缀)* 中间的内容 *斜体(后缀)*]]
100+
101+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|~~删除线(前缀)~~ 中间的内容 ~~删除线(后缀)~~]]
102+
103+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|<span style="color: red;">自定义 HTML</span>]]
104+
105+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|<span style="color: red;">自定义 HTML</span> 中间的内容 <span style="color: blue;">自定义 HTML</span>]]
106+
```
107+
108+
效果
109+
110+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|`代码块(前缀)` 中间的内容 `代码块(后缀)`]]
111+
112+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|**粗体(前缀)** 中间的内容 **粗体(后缀)**]]
113+
114+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|*斜体(前缀)* 中间的内容 *斜体(后缀)*]]
115+
116+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|~~删除线(前缀)~~ 中间的内容 ~~删除线(后缀)~~]]
117+
118+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|<span style="color: red;">自定义 HTML</span>]]
119+
120+
[[pages/zh-CN/integrations/markdown-it-bi-directional-links/双向链接示例页面|<span style="color: red;">自定义 HTML</span> 中间的内容 <span style="color: blue;">自定义 HTML</span>]]
121+
92122
## 如何使用
93123

94124
### 安装

packages/markdown-it-bi-directional-links/src/index.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { basename, extname, join, relative } from 'node:path'
22
import fg from 'fast-glob'
33
import type { PluginSimple } from 'markdown-it'
4+
import type Token from 'markdown-it/lib/token'
45

56
const biDirectionalLinkPattern = /\[\[([^|\]\n]+)(\|([^\]\n]+))?\]\](?!\()/
67
const biDirectionalLinkPatternWithStart = /^\[\[([^|\]\n]+)(\|([^\]\n]+))?\]\](?!\()/
@@ -117,9 +118,31 @@ export const BiDirectionalLinks: (options: {
117118
const openToken = state.push('link_open', 'a', 1)
118119
openToken.attrSet('href', resolvedNewHref)
119120

120-
// text
121-
const textToken = state.push('text', '', 0)
122-
textToken.content = text || href
121+
// Final inline tokens for link content
122+
const linkTokenChildrenContent: Token[] = []
123+
124+
// Produces a set of inline tokens and each contains a set of children tokens
125+
const parsedInlineTokens = text ? md.parseInline(text, null) : md.parseInline(href, null) || []
126+
127+
// We are going to push the children tokens of each inline token to the final inline tokens
128+
// Need to check if the parsed inline tokens have children tokens
129+
if (parsedInlineTokens && parsedInlineTokens.length) {
130+
parsedInlineTokens.forEach((tokens) => {
131+
if (!tokens.children)
132+
return
133+
134+
// If the inline token has children tokens, push them to the final inline tokens one by one
135+
tokens.children.forEach((token) => {
136+
linkTokenChildrenContent.push(token)
137+
})
138+
})
139+
}
140+
141+
// Push the final inline tokens to the state
142+
for (const token of linkTokenChildrenContent) {
143+
const pushedToken = state.push(token.type, token.tag, token.nesting)
144+
pushedToken.content = token.content
145+
}
123146

124147
// and link_close tokens
125148
state.push('link_close', 'a', -1)

0 commit comments

Comments
 (0)