-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathMdAttributeRenderer.ts
204 lines (168 loc) · 6.36 KB
/
MdAttributeRenderer.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import has from 'lodash/has';
import { getVslotShorthandName } from './vueSlotSyntaxProcessor';
import type { MarkdownProcessor } from './MarkdownProcessor';
import * as logger from '../utils/logger';
import { createSlotTemplateNode } from './elements';
import { MbNode, NodeOrText, parseHTML } from '../utils/node';
const _ = {
has,
};
/**
* Class that is responsible for rendering markdown-in-attributes
*/
export class MdAttributeRenderer {
markdownProcessor: MarkdownProcessor;
constructor(mdp: MarkdownProcessor) {
this.markdownProcessor = mdp;
}
/**
* Processes the markdown attribute of the provided element, inserting the corresponding <slot> child
* if there is no pre-existing slot child with the name of the attribute present.
* @param node Element to process
* @param attribute Attribute name to process
* @param isInline Whether to process the attribute with only inline markdown-it rules
* @param slotName Name attribute of the <slot> element to insert, which defaults to the attribute name
*/
processAttributeWithoutOverride(node: MbNode, attribute: string,
isInline: boolean, slotName = attribute): void {
const hasAttributeSlot = node.children
&& node.children.some(child => getVslotShorthandName(child) === slotName);
if (!hasAttributeSlot && _.has(node.attribs, attribute)) {
let rendered;
if (isInline) {
rendered = this.markdownProcessor.renderMdInline(node.attribs[attribute]);
} else {
rendered = this.markdownProcessor.renderMd(node.attribs[attribute]);
}
const attributeSlotElement: NodeOrText[] = createSlotTemplateNode(slotName, rendered);
node.children = node.children
? attributeSlotElement.concat(node.children)
: attributeSlotElement;
}
delete node.attribs[attribute];
}
/**
* Checks if the node has both the given slot and the given attribute,
* deleting the attribute and logging a warning if both the slot and attribute exist.
* @param node Element to process
* @param attribute Attribute name to process
* @param slotName Name attribute of the <slot> element to insert, which defaults to the attribute name
* @returns {boolean} whether the node has both the slot and attribute
*/
// eslint-disable-next-line class-methods-use-this
hasSlotOverridingAttribute(node: MbNode, attribute: string, slotName = attribute): boolean {
const hasNamedSlot = node.children
&& node.children.some(child => getVslotShorthandName(child) === slotName);
if (!hasNamedSlot || !node.attribs) {
return false;
}
// If the slot is present, remove the attribute as the attribute has no effect.
const hasAttribute = _.has(node.attribs, attribute);
if (hasAttribute) {
logger.warn(`${node.name} has a ${slotName} slot, '${attribute}' attribute has no effect.`);
delete node.attribs[attribute];
}
return hasAttribute;
}
/**
* Checks if there is a pre-existing slot for the attribute.
* If there is a slot, it deletes the attribute and logs a warning.
* If there is no slot, it processes the markdown attribute.
* @param node Element to process
* @param attribute Attribute name to process
* @param isInline Whether to process the attribute with only inline markdown-it rules
* @param slotName Name attribute of the <slot> element to insert, which defaults to the attribute name
*/
processSlotAttribute(node: MbNode, attribute: string, isInline: boolean, slotName = attribute) {
if (!this.hasSlotOverridingAttribute(node, attribute, slotName)) {
this.processAttributeWithoutOverride(node, attribute, isInline, slotName);
}
}
processPopoverAttributes(node: MbNode) {
this.processSlotAttribute(node, 'header', true);
// Warn if there is a content slot overriding the attributes 'content' or 'src'
const hasSlotAndContentAttribute = this.hasSlotOverridingAttribute(node, 'content', 'content');
const hasSlotAndSrcAttribute = this.hasSlotOverridingAttribute(node, 'src', 'content');
if (hasSlotAndContentAttribute || hasSlotAndSrcAttribute) {
return;
}
if (_.has(node.attribs, 'content') && _.has(node.attribs, 'src')) {
logger.warn(`${node.name} has a 'content' attribute, 'src' attribute has no effect.`);
delete node.attribs.src;
}
this.processAttributeWithoutOverride(node, 'content', true);
}
processTooltip(node: MbNode) {
this.processSlotAttribute(node, 'content', true);
}
processModalAttributes(node: MbNode) {
this.processSlotAttribute(node, 'header', true);
}
/*
* Panels
*/
processPanelAttributes(node: MbNode) {
this.processSlotAttribute(node, 'alt', false, '_alt');
this.processSlotAttribute(node, 'header', false);
}
/*
* Questions, QOption, and Quizzes
*/
processQuestion(node: MbNode) {
this.processSlotAttribute(node, 'header', false);
this.processSlotAttribute(node, 'hint', false);
this.processSlotAttribute(node, 'answer', false);
}
processQOption(node: MbNode) {
this.processSlotAttribute(node, 'reason', false);
}
processQuiz(node: MbNode) {
this.processSlotAttribute(node, 'intro', false);
}
/*
* Tabs
*/
processTabAttributes(node: MbNode) {
this.processSlotAttribute(node, 'header', true);
}
/*
* Boxes
*/
processBoxAttributes(node: MbNode) {
this.processSlotAttribute(node, 'icon', true);
this.processSlotAttribute(node, 'header', false);
}
/*
* Dropdowns
*/
processDropdownAttributes(node: MbNode) {
this.processSlotAttribute(node, 'header', true);
}
/**
* Thumbnails
*/
processThumbnailAttributes(node: MbNode) {
if (!node.attribs) {
return;
}
const isImage = _.has(node.attribs, 'src') && node.attribs.src !== '';
if (isImage) {
return;
}
const text = _.has(node.attribs, 'text') ? node.attribs.text : '';
if (text === '') {
return;
}
const renderedText = this.markdownProcessor.renderMdInline(text);
node.children = parseHTML(renderedText);
delete node.attribs.text;
}
processScrollTopButtonAttributes(node: MbNode) {
this.processSlotAttribute(node, 'icon', true);
}
processAnnotationPointAttributes(node: MbNode) {
this.processSlotAttribute(node, 'content', false);
this.processSlotAttribute(node, 'header', false);
this.processSlotAttribute(node, 'label', false);
}
}