2020-09-28 05:14:00 -07:00
|
|
|
<script>
|
2020-09-28 08:51:26 -07:00
|
|
|
import Icon from './Icon';
|
2021-02-01 12:03:30 -08:00
|
|
|
import spelling from "../plugins/spelling";
|
2020-09-28 08:51:26 -07:00
|
|
|
|
2020-09-28 05:14:00 -07:00
|
|
|
export default {
|
2021-02-01 12:03:30 -08:00
|
|
|
mixins: [ spelling ],
|
2020-09-28 05:14:00 -07:00
|
|
|
props: {
|
|
|
|
text: { required: true },
|
2021-07-02 13:32:23 -07:00
|
|
|
noicons: { type: Boolean },
|
2021-09-28 12:34:25 -07:00
|
|
|
escape: { type: Boolean },
|
2020-09-28 05:14:00 -07:00
|
|
|
},
|
|
|
|
render(h) {
|
2021-09-28 12:34:25 -07:00
|
|
|
let text = this.text;
|
|
|
|
if (this.escape) {
|
|
|
|
text = text.replace(/[&<>"]/g, tag => escapeChars[tag] || tag);
|
|
|
|
}
|
2020-09-28 08:51:26 -07:00
|
|
|
if (!this.text) {
|
|
|
|
return h('span');
|
|
|
|
}
|
|
|
|
|
2020-09-28 05:14:00 -07:00
|
|
|
let isLink = false;
|
2020-09-28 08:51:26 -07:00
|
|
|
let isIcon = false;
|
2021-01-01 10:47:22 -08:00
|
|
|
let isEscape = false;
|
2020-09-28 05:14:00 -07:00
|
|
|
let buffer = '';
|
|
|
|
let linkBuffer = '';
|
|
|
|
const children = [];
|
|
|
|
const buildLink = _ => {
|
2020-09-28 08:51:26 -07:00
|
|
|
if (isIcon) {
|
2020-12-30 06:33:26 -08:00
|
|
|
return h(Icon, {props: { v: buffer }});
|
2020-09-28 08:51:26 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 08:52:21 -08:00
|
|
|
const bufferNode = [ h('span', {domProps: { innerHTML: this.handleSpelling(buffer) }}) ];
|
2020-10-12 12:07:10 -07:00
|
|
|
|
2020-09-28 05:14:00 -07:00
|
|
|
if (!isLink) {
|
2020-10-12 12:07:10 -07:00
|
|
|
return bufferNode;
|
2020-09-28 05:14:00 -07:00
|
|
|
}
|
|
|
|
|
2021-07-03 03:24:55 -07:00
|
|
|
if (linkBuffer === '') {
|
|
|
|
linkBuffer = '#' + buffer;
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:07:10 -07:00
|
|
|
if (linkBuffer.indexOf('https://') === 0
|
|
|
|
|| linkBuffer.indexOf('http://') === 0
|
|
|
|
|| linkBuffer.indexOf('mailto:') === 0
|
|
|
|
) {
|
2020-09-28 05:14:00 -07:00
|
|
|
return h(
|
|
|
|
'a',
|
|
|
|
{domProps: {href: linkBuffer, target: '_blank', rel: 'noopener'}},
|
2020-10-12 12:07:10 -07:00
|
|
|
bufferNode,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linkBuffer.indexOf('#') === 0) {
|
|
|
|
return h(
|
|
|
|
'a',
|
|
|
|
{domProps: {href: linkBuffer}},
|
|
|
|
bufferNode,
|
2020-09-28 08:51:26 -07:00
|
|
|
);
|
2020-09-28 05:14:00 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 08:52:21 -08:00
|
|
|
return h('nuxt-link', {props: { to: linkBuffer || '/' + this.config.nouns.route + '#' + this.handleSpelling(buffer) }}, bufferNode);
|
2020-09-28 05:14:00 -07:00
|
|
|
}
|
|
|
|
const addChild = _ => {
|
|
|
|
if (!buffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
children.push(buildLink());
|
|
|
|
buffer = '';
|
|
|
|
linkBuffer = '';
|
|
|
|
}
|
|
|
|
for (let c of this.text) {
|
|
|
|
if (c === '{') {
|
|
|
|
addChild();
|
|
|
|
isLink = true;
|
|
|
|
continue;
|
|
|
|
} else if (c === '}') {
|
|
|
|
addChild();
|
|
|
|
isLink = false;
|
|
|
|
continue;
|
|
|
|
} else if (isLink && c === '=') {
|
2020-10-10 09:31:31 -07:00
|
|
|
if (linkBuffer) {
|
|
|
|
linkBuffer += '='
|
|
|
|
}
|
|
|
|
linkBuffer += buffer;
|
2020-09-28 05:14:00 -07:00
|
|
|
buffer = '';
|
|
|
|
continue;
|
2021-07-02 13:32:23 -07:00
|
|
|
} else if (c === '[' && !this.noicons) {
|
2020-09-28 08:51:26 -07:00
|
|
|
addChild();
|
2021-01-01 10:47:22 -08:00
|
|
|
if (isEscape) {
|
|
|
|
isEscape = false;
|
|
|
|
} else {
|
|
|
|
isIcon = true;
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-02 13:32:23 -07:00
|
|
|
} else if (c === ']' && !this.noicons) {
|
2020-09-28 08:51:26 -07:00
|
|
|
addChild();
|
2021-01-01 10:47:22 -08:00
|
|
|
if (isIcon) {
|
|
|
|
isIcon = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (c === '\\') {
|
|
|
|
isEscape = true;
|
2020-09-28 08:51:26 -07:00
|
|
|
continue;
|
2021-01-01 10:47:22 -08:00
|
|
|
} else if (isEscape) {
|
|
|
|
buffer += '\\';
|
|
|
|
isEscape = false;
|
2020-09-28 05:14:00 -07:00
|
|
|
}
|
|
|
|
buffer += c;
|
|
|
|
}
|
|
|
|
addChild();
|
|
|
|
|
|
|
|
return h('span', children);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|