This commit is contained in:
Le Ma
2024-07-13 14:41:05 +12:00
parent 535bfa5578
commit 25f6111a95
2 changed files with 155 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
import { createRouter, createWebHistory } from 'vue-router'
import EditNote from "@/views/EditNote.vue";
import ViewNote from "@/views/ViewNote.vue";
import ViewMdNote from "@/views/ViewMdNote.vue";
import ErrorRoute from "@/views/ErrorRoute.vue";
import ErrorNote from "@/views/ErrorNote.vue";
import { getNoteMeta } from "@/api/note";
@@ -32,18 +33,25 @@ function getNoteView() {
let key = path.substring(1, path.length);
keyMeta = storage.local.getObject(key+".keyMeta");
let isMd = path.endsWith(".md");
if (isMd) {
key = path.substring(1, path.length - 3);
}
keyMeta = storage.local.getObject(key + ".keyMeta");
if (keyMeta && keyMeta.key) {
key = keyMeta.key;
}
let regKey = /^[abcdefhikmnopqstuvwxyz23456789]{16}$/;
if (!regKey.test(key)) {
errorMeta = 100002;
return ErrorNote;
}
if (keyMeta && keyMeta.key) {
if (keyMeta && keyMeta.key && !isMd) {
//firstEdit
return EditNote;
}
@@ -65,8 +73,12 @@ function getNoteView() {
//validated state
if (1 == noteMeta.state) {
if(isMd){
return ViewMdNote;
}else{
return ViewNote;
}
}
//deleted
if (0 == noteMeta.state) {
@@ -113,6 +125,12 @@ const routes = [
component: getNoteView(),
meta: { keyMeta: getKeyMetaParam(), noteMeta: getNoteMetaParam(), errorMeta: getErrorMetaParam() },
},
{
path: "/:name([a-z0-9]{10,20}).md",
name: "md",
component: getNoteView(),
meta: { keyMeta: getKeyMetaParam(), noteMeta: getNoteMetaParam(), errorMeta: getErrorMetaParam() },
},
{ path: "/:path(.*)", component: ErrorRoute }
];

134
src/views/ViewMdNote.vue Normal file
View File

@@ -0,0 +1,134 @@
<template>
<div class="layout">
<div v-html="noteForm.renderedMarkdown"></div>
</div>
</template>
<script>
import { md5, unwrap } from "@/libs/secret";
import { getStoreKey } from "@/api/lock";
import { getNoteBlob } from "@/api/note";
import storage from "@/libs/storage";
import markdownit from 'markdown-it'
const md = markdownit()
export default {
name: 'ViewMdNote',
components: {},
data() {
return {
noteForm: {
url: '',
noteUrl: '',
text: '',
renderedMarkdown: '',
key: '',
},
secret: {
storeKey: '',
secretKey: '',
cipher: '',
md5: '',
},
state: {
lock: null,
initTime: null,
initTtl: null,
ttl: null,
ttlDesc: '-- : --',
serverTime: null,
},
model: {
showError: false,
},
errorInfo: '',
}
},
created() {
// read $route
this.noteForm.key = this.$route.params.name;
let noteMeta = this.$route.meta.noteMeta;
this.secret.secretKey = noteMeta.secretKey;
this.secret.storeKey = getStoreKey(this.noteForm.key);
if (noteMeta) {
this.secret.cipher = "00000000000000000000000000000000";//noteMeta.cipher; //读者有没有值可配置
this.secret.md5 = noteMeta.md5;
storage.local.dynamicClear();
this.loadText();
} else {
this.errorInfo = this.$t('error.100001');
this.model.showError = true;
return false;
}
this.noteForm.renderedMarkdown = md.render(this.noteForm.text);
}, mounted() {
},
methods: {
loadText() {
let that = this;
let storeInfo = storage.local.getText(this.secret.storeKey);
let starray = [];
if (storeInfo) {
starray = storeInfo.split('|');
}
if (!storeInfo || !starray[4] || (md5(starray[4]) != this.secret.md5)) {
// local is useless
getNoteBlob(this.noteForm.key).then((res) => {
if (!res.data || res.data.size == 0) {
return;
}
res.data.arrayBuffer().then(function (ab) {
if (!ab || ab.byteLength == 0) {
return;
}
let base64String = Buffer.from(ab).toString('base64')
that.noteForm.text = unwrap(base64String, that.secret.secretKey);
// if local is enough, set local
if (storage.local.getAvailableSize() > 1 * 1024 * 1024) {
storage.local.setText(that.secret.storeKey, that.state.lock + '|' + that.secret.cipher + '|1|' + that.state.serverTime + '|' + base64String);
}
});
});
} else {
starray = storeInfo.split('|');
if (!starray[4]) {
return;
}
this.noteForm.text = unwrap(starray[4], this.secret.secretKey);
// local is usable, and set commited flag
if ("0" == starray[2]) {
storage.local.setText(this.secret.storeKey, starray[0] + "|" + starray[1] + "|1|" + starray[3] + "|" + starray[4]);
}
}
},
}
}
</script>
<style scoped>
body,div {
margin:auto;
padding:auto;
}
</style>