pleshevski.ru/old/uikit/link.ts

20 lines
480 B
TypeScript

import { AnyNode, Attrs, E } from "ren/node.ts";
export function Link(
text: string,
sourceAttrs: Attrs | Attrs[],
): AnyNode {
const attrs = Array.isArray(sourceAttrs) ? sourceAttrs : [sourceAttrs];
const isExternal = attrs.some((attr) =>
typeof attr.href === "string" && attr.href?.startsWith("http")
);
if (isExternal) {
attrs.push({
target: "_blank",
rel: "external nofollow noopener noreferrer",
});
}
return E("a", attrs, text);
}