@fedify/markdown-it-mention
Version:
A markdown-it plugin that parses and renders Mastodon-style @mentions.
41 lines (40 loc) • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBareHandle = toBareHandle;
exports.toFullHandle = toFullHandle;
const mod_js_1 = require("../deps/jsr.io/@std/html/0.224.2/mod.js");
/**
* Converts a full handle to a bare handle.
* @param handle The full handle.
* @param env The environment.
* @returns The bare handle without the domain in HTML.
*/
// deno-lint-ignore no-explicit-any
function toBareHandle(handle, _env) {
const idx = handle.indexOf("@", 1);
if (idx < 0) {
return `<span class="at">@</span><span class="user">${(0, mod_js_1.escape)(handle.startsWith("@") ? handle.substring(1) : handle)}</span>`;
}
let user = handle.substring(0, idx);
if (user.startsWith("@"))
user = user.substring(1);
return `<span class="at">@</span><span class="user">${(0, mod_js_1.escape)(user)}</span>`;
}
/**
* Does nothing, but just returns the full handle in HTML.
* @param handle The full handle.
* @param env The environment.
* @returns The full handle.
*/
// deno-lint-ignore no-explicit-any
function toFullHandle(handle, _env) {
const idx = handle.indexOf("@", 1);
if (idx < 0) {
return `<span class="at">@</span><span class="user">${(0, mod_js_1.escape)(handle.startsWith("@") ? handle.substring(1) : handle)}</span>`;
}
let user = handle.substring(0, idx);
if (user.startsWith("@"))
user = user.substring(1);
const domain = handle.substring(idx + 1);
return `<span class="at">@</span><span class="user">${(0, mod_js_1.escape)(user)}</span><span class="at">@</span><span class="domain">${(0, mod_js_1.escape)(domain)}</span>`;
}