UNPKG

remark-linkify

Version:

A remark plugin to automatically convert URLs and email addresses into links.

82 lines (80 loc) 2.98 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { remarkLinkify: () => remarkLinkify }); module.exports = __toCommonJS(index_exports); var import_linkify_it = __toESM(require("linkify-it")); var import_unist_util_visit = require("unist-util-visit"); var linkify = new import_linkify_it.default(); function remarkLinkify() { return (tree) => { (0, import_unist_util_visit.visit)( tree, "text", (node, index, parent) => { if (!parent || index === void 0 || parent.type === "link" || parent.type === "linkReference" || !node.value) { return; } const text = node.value; const matches = linkify.match(text); if (!matches || matches.length === 0) { return; } const newNodes = []; let lastIndex = 0; for (const match of matches) { if (match.index > lastIndex) { newNodes.push({ type: "text", value: text.slice(lastIndex, match.index) }); } newNodes.push({ type: "link", url: match.url, children: [{ type: "text", value: match.text }] }); lastIndex = match.lastIndex; } if (lastIndex < text.length) { newNodes.push({ type: "text", value: text.slice(lastIndex) }); } parent.children.splice(index, 1, ...newNodes); return index + newNodes.length; } ); }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { remarkLinkify });