This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/components/LinkedText.vue

107 lines
3.2 KiB
Vue
Raw Normal View History

2020-09-28 05:14:00 -07:00
<script>
import Icon from './Icon';
import {mapState} from "vuex";
import zhConverter from 'zh_cn_zh_tw';
2020-09-28 05:14:00 -07:00
export default {
props: {
text: { required: true },
},
render(h) {
if (!this.text) {
return h('span');
}
2020-09-28 05:14:00 -07:00
let isLink = false;
let isIcon = false;
2020-09-28 05:14:00 -07:00
let buffer = '';
let linkBuffer = '';
const children = [];
const buildLink = _ => {
if (isIcon) {
return h(Icon, {props: { v: buffer}});
}
const bufferNode = [ h('span', {domProps: { innerHTML: this.handleSpelling(buffer) }}) ];
2020-09-28 05:14:00 -07:00
if (!isLink) {
return bufferNode;
2020-09-28 05:14:00 -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'}},
bufferNode,
);
}
if (linkBuffer.indexOf('#') === 0) {
return h(
'a',
{domProps: {href: linkBuffer}},
bufferNode,
);
2020-09-28 05:14:00 -07: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;
} else if (c === '[') {
addChild();
isIcon = true;
continue;
} else if (c === ']') {
addChild();
isIcon = false;
continue;
2020-09-28 05:14:00 -07:00
}
buffer += c;
}
addChild();
return h('span', children);
},
computed: {
...mapState([
'spelling',
]),
},
methods: {
handleSpelling(str) {
if (this.config.locale !== 'zh' || this.spelling === 'traditional') {
return str;
}
return zhConverter.convertToSimplifiedChinese(str);
}
},
2020-09-28 05:14:00 -07:00
}
</script>