mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-04 15:29:29 +08:00
112 lines
2.6 KiB
Text
112 lines
2.6 KiB
Text
/**
|
||
* @typedef {import('mdast').Link} Link
|
||
* @typedef {import('../types.js').Handle} Handle
|
||
* @typedef {import('../types.js').Exit} Exit
|
||
*/
|
||
|
||
import {checkQuote} from '../util/check-quote.js'
|
||
import {formatLinkAsAutolink} from '../util/format-link-as-autolink.js'
|
||
import {containerPhrasing} from '../util/container-phrasing.js'
|
||
import {safe} from '../util/safe.js'
|
||
import {track} from '../util/track.js'
|
||
|
||
link.peek = linkPeek
|
||
|
||
/**
|
||
* @type {Handle}
|
||
* @param {Link} node
|
||
*/
|
||
export function link(node, _, context, safeOptions) {
|
||
const quote = checkQuote(context)
|
||
const suffix = quote === '"' ? 'Quote' : 'Apostrophe'
|
||
const tracker = track(safeOptions)
|
||
/** @type {Exit} */
|
||
let exit
|
||
/** @type {Exit} */
|
||
let subexit
|
||
|
||
if (formatLinkAsAutolink(node, context)) {
|
||
// Hide the fact that we’re in phrasing, because escapes don’t work.
|
||
const stack = context.stack
|
||
context.stack = []
|
||
exit = context.enter('autolink')
|
||
let value = tracker.move('<')
|
||
value += tracker.move(
|
||
containerPhrasing(node, context, {
|
||
before: value,
|
||
after: '>',
|
||
...tracker.current()
|
||
})
|
||
)
|
||
value += tracker.move('>')
|
||
exit()
|
||
context.stack = stack
|
||
return value
|
||
}
|
||
|
||
exit = context.enter('link')
|
||
subexit = context.enter('label')
|
||
let value = tracker.move('[')
|
||
value += tracker.move(
|
||
containerPhrasing(node, context, {
|
||
before: value,
|
||
after: '](',
|
||
...tracker.current()
|
||
})
|
||
)
|
||
value += tracker.move('](')
|
||
subexit()
|
||
|
||
if (
|
||
// If there’s no url but there is a title…
|
||
(!node.url && node.title) ||
|
||
// If there are control characters or whitespace.
|
||
/[\0- \u007F]/.test(node.url)
|
||
) {
|
||
subexit = context.enter('destinationLiteral')
|
||
value += tracker.move('<')
|
||
value += tracker.move(
|
||
safe(context, node.url, {before: value, after: '>', ...tracker.current()})
|
||
)
|
||
value += tracker.move('>')
|
||
} else {
|
||
// No whitespace, raw is prettier.
|
||
subexit = context.enter('destinationRaw')
|
||
value += tracker.move(
|
||
safe(context, node.url, {
|
||
before: value,
|
||
after: node.title ? ' ' : ')',
|
||
...tracker.current()
|
||
})
|
||
)
|
||
}
|
||
|
||
subexit()
|
||
|
||
if (node.title) {
|
||
subexit = context.enter('title' + suffix)
|
||
value += tracker.move(' ' + quote)
|
||
value += tracker.move(
|
||
safe(context, node.title, {
|
||
before: value,
|
||
after: quote,
|
||
...tracker.current()
|
||
})
|
||
)
|
||
value += tracker.move(quote)
|
||
subexit()
|
||
}
|
||
|
||
value += tracker.move(')')
|
||
|
||
exit()
|
||
return value
|
||
}
|
||
|
||
/**
|
||
* @type {Handle}
|
||
* @param {Link} node
|
||
*/
|
||
function linkPeek(node, _, context) {
|
||
return formatLinkAsAutolink(node, context) ? '<' : '['
|
||
}
|