Urara-Blog/node_modules/.pnpm-store/v3/files/d1/51434827e441a3b76c09d6c644bc5a5bc4845d462bd17e4312b0610c78544f605f40570761e2eead6fc41dcef86b400619b32d4a2512e3a0c963f5b0fe04be
2022-08-14 01:14:53 +08:00

112 lines
2.6 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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 were in phrasing, because escapes dont 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 theres 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) ? '<' : '['
}