-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutils.ts
95 lines (79 loc) · 2.84 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { sep } from 'node:path'
import type Token from 'markdown-it/lib/token'
import type StateInline from 'markdown-it/lib/rules_inline/state_inline'
import type MarkdownIt from 'markdown-it'
/**
* Find / resolve bi-directional links.
* @param possibleBiDirectionalLinksInFilePaths - file path is the key
* @param possibleBiDirectionalLinksInFullFilePaths - full file path is the key
* @param href - URL style file name representation
*/
export function findBiDirectionalLinks(
possibleBiDirectionalLinksInFilePaths: Record<string, string>,
possibleBiDirectionalLinksInFullFilePaths: Record<string, string>,
href: string,
) {
if (!href)
return null
if (href.includes(sep))
return possibleBiDirectionalLinksInFullFilePaths[href]
return possibleBiDirectionalLinksInFilePaths[href]
}
export function genLink(
state: StateInline,
resolvedNewHref: string,
text: string,
md: MarkdownIt,
href: string,
link: RegExpMatchArray,
) {
// Create new link_open
const openToken = state.push('link_open', 'a', 1)
openToken.attrSet('href', resolvedNewHref)
// Final inline tokens for link content
const linkTokenChildrenContent: Token[] = []
// Remove the search and hash from the href
const parsedUrl = new URL(href, 'https://a.com')
const hrefWithoutSearchAndHash = decodeURIComponent(parsedUrl.pathname.slice(1))
// Produces a set of inline tokens and each contains a set of children tokens
const parsedInlineTokens = text
? md.parseInline(text, state.env)
: md.parseInline(hrefWithoutSearchAndHash, state.env) || []
// We are going to push the children tokens of each inline token to the final inline tokens
// Need to check if the parsed inline tokens have children tokens
if (parsedInlineTokens && parsedInlineTokens.length) {
parsedInlineTokens.forEach((tokens) => {
if (!tokens.children)
return
// If the inline token has children tokens, push them to the final inline tokens one by one
tokens.children.forEach((token) => {
linkTokenChildrenContent.push(token)
})
})
}
// Push the final inline tokens to the state
for (const token of linkTokenChildrenContent) {
const pushedToken = state.push(token.type, token.tag, token.nesting)
pushedToken.content = token.content
}
// and link_close tokens
state.push('link_close', 'a', -1)
// Update the position in the source string
state.pos += link![0].length
}
export function genImage(
state: StateInline,
resolvedNewHref: string,
text: string,
link: RegExpMatchArray,
) {
const openToken = state.push('image', 'img', 1)
openToken.attrSet('src', resolvedNewHref)
openToken.attrSet('alt', '')
openToken.children = []
openToken.content = text
const innerTextToken = state.push('text', '', 0)
innerTextToken.content = text
openToken.children.push(innerTextToken)
state.pos += link![0].length
}