-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathicon-macro.js
More file actions
61 lines (57 loc) · 1.68 KB
/
icon-macro.js
File metadata and controls
61 lines (57 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This macro relies on the material-icons font and the lucide icons font being loaded in UI bundle.
*
* @example Material Icon
* icon:material:menu-open[]
*
* @example Lucide Icon
* icon:boom-box[]
*/
function inlineIconMacro() {
return function () {
this.process((parent, target, attrs) => {
if (target.startsWith("material:")) {
iconTarget = target
.replace("material:", "")
.trim()
.replace("-", "_");
return this.createInlinePass(
parent,
`<i ${htmlAttrs(attrs, "material-icons")}>${iconTarget}</i>` + renderName(attrs?.name)
);
} else {
iconTarget = target
.replace("lucide:", "")
.trim()
return this.createInlinePass(
parent,
`<i ${htmlAttrs(attrs, `icon-${iconTarget}`)}></i>` + renderName(attrs?.name)
);
}
});
};
}
function renderName(name) {
if (!name) return "";
return ` <b>${name}</b>`;
}
function htmlAttrs({ size, role, alt, title, ariaLabel, $positional = [] }, klass) {
const [posSize] = $positional;
return [
(size || posSize) && `style="font-size: ${(size || posSize).replace("px", "").trim()}px;"`,
(role || klass) && `class="${[klass, role].filter(Boolean).join(" ")}"`,
title && `title="${title}"`,
(alt || ariaLabel) && `aria-label="${alt || ariaLabel}" role="img"`,
!(alt || ariaLabel) && "aria-hidden='true'",
]
.filter(Boolean)
.join(" ");
}
/**
* @param { import("@asciidoctor/core/types").Asciidoctor.Extensions.Registry } registry
* @param context
*/
function register(registry) {
registry.inlineMacro("icon", inlineIconMacro());
}
module.exports.register = register;