Markdown displays rendered html by default.

This commit is contained in:
Aicirou 2020-06-30 01:49:13 +08:00
parent 7c2de830b6
commit dbab8e23e8
3 changed files with 71 additions and 22 deletions

View File

@ -52,10 +52,23 @@ export default {
console.log("not mounted init"); console.log("not mounted init");
} }
}, },
watch: {
source(val) {
if (val) {
this.initWithMd();
}
},
},
methods: { methods: {
handler(source) {
if (source) {
source = source.replace(/---\n([\s\S]*)---\n/, "");
}
return source;
},
// 使 md // 使 md
initWithMd() { initWithMd() {
this.markedHTML = this.marked(this.source); this.markedHTML = this.marked(this.handler(this.source));
}, },
// 使 url // 使 url
async initWithUrl() { async initWithUrl() {

View File

@ -1,7 +1,7 @@
<template> <template>
<div> <div>
<nav <nav
class="breadcrumb level g2-breadcrumb is-hidden-mobile is-hidden-touch" class="breadcrumb level g2-breadcrumb is-hidden-mobile"
aria-label="breadcrumbs" aria-label="breadcrumbs"
> >
<div class="level-left"> <div class="level-left">

View File

@ -1,31 +1,53 @@
<template> <template>
<div class="content g2-content"> <div class="content g2-content">
<codemirror v-model="content" :options="cmOptions" /> <codemirror
v-if="!ismd || isEdit || loaded"
v-show="!ismd || isEdit"
v-model="content"
:options="options"
/>
<markdown
v-if="ismd"
v-show="!isEdit"
:source="content"
/>
<a
v-if="ismd"
class="g2-content-edit is-hidden-mobile"
@click="edit"
>
<i
:class="'fa' + (isEdit ? ' fa-eye' : ' fa-pencil-square-o')"
aria-hidden="true"
></i>
</a>
</div> </div>
</template> </template>
<script> <script>
import { get_file, decode64 } from "@utils/AcrouUtil"; import { get_file, decode64 } from "@utils/AcrouUtil";
import { codemirror } from 'vue-codemirror' import { codemirror } from "vue-codemirror";
// import base style // import base style
import 'codemirror/lib/codemirror.css' import "codemirror/lib/codemirror.css";
// import language js // import language js
import 'codemirror/mode/javascript/javascript.js' import "codemirror/mode/javascript/javascript.js";
// import theme style // import theme style
import 'codemirror/theme/base16-dark.css' import "codemirror/theme/base16-dark.css";
export default { export default {
data: function() { data: function() {
return { return {
path: "", path: "",
content: "", content: "",
cmOptions: { options: {
tabSize: 4, tabSize: 4,
mode: 'text/javascript', mode: "text/javascript",
// theme: 'base16-dark', // theme: 'base16-dark',
lineNumbers: true, lineNumbers: true,
line: true line: true,
} },
isEdit: false,
loaded: false,
}; };
}, },
activated() { activated() {
@ -36,21 +58,28 @@ export default {
if (this.$route.params.path) { if (this.$route.params.path) {
return decode64(this.$route.params.path); return decode64(this.$route.params.path);
} }
return '' return "";
} },
ismd() {
return this.url.match(".*\\.(md|MD)$");
},
}, },
components: { components: {
codemirror codemirror,
}, },
methods: { methods: {
render() { render() {
let path = this.url; let path = this.url;
this.content = this.$t("page.text.loading"); this.content = this.$t("page.text.loading");
get_file({ path: path, file: {} }, data => { get_file({ path: path, file: {} }, (data) => {
this.content = data; this.content = data;
}); });
} },
} edit() {
this.loaded = true;
this.isEdit = !this.isEdit;
},
},
}; };
</script> </script>
<style lang="scss"> <style lang="scss">
@ -58,3 +87,10 @@ export default {
height: 650px; height: 650px;
} }
</style> </style>
<style lang="scss" scoped>
.g2-content-edit {
position: absolute;
top: 10px;
right: 0;
}
</style>