shelving
Version:
Toolkit for using data in JavaScript.
29 lines (28 loc) • 2.01 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { formatURI } from "../../util/format.js";
import { getRegExp } from "../../util/regexp.js";
import { createMarkupRule } from "../MarkupRule.js";
/** Render `<a href="">` if the link is a valid one, or `<a>` (with no `href`) if it isn't. */
function _renderLink(key, { title, href }, parser) {
const link = parser.getLink(href);
return (_jsx("a", { href: link?.href, rel: parser.rel, children: title ? parser.parse(title, "link") : link ? formatURI(link, _renderLink) : "" }, key));
}
/**
* Markdown-style link.
* - Link in standard Markdown format, e.g. `[Google Maps](http://google.com/maps)`
* - If no title is specified a cleaned up version of the URL will be used, e.g. `google.com/maps`
* - Does not need space before/after the link.
* - If link is not valid (using `new URL(url)` then unparsed text will be returned.
* - For security only schemes that appear in `MarkupOptions.schemes` will match (defaults to `http:` and `https:`).
*/
export const LINK_RULE = createMarkupRule(getRegExp(/\[(?<title>[^\]\n]*?)\]\((?<href>[^)\n]*?)\)/), //
_renderLink, ["inline", "list"]);
/**
* Autolinked URL starts with `scheme:` (any scheme in `MarkupOptions.schemes`) and matches an unlimited number of non-space characters.
* - If followed by space and then text in `()` round or `[]` square brackets that will be used as the title, e.g. `http://google.com/maps (Google Maps)` or `http://google.com/maps [Google Maps]` (this syntax is from Todoist and maybe other things too).
* - If no title is specified a cleaned up version of the URL will be used, e.g. `google.com/maps`
* - If link is not valid (using `new URL(url)` then unparsed text will be returned.
* - For security only schemes that appear in `MarkupOptions.schemes` will match (defaults to `http:` and `https:`).
*/
export const AUTOLINK_RULE = createMarkupRule(getRegExp(/(?<href>[a-z]{3,}?:\S+)(?: +(?:\((?<title>[^)\n]*?)\)))?/), //
_renderLink, ["inline", "list"]);