Skip to content

Commit

Permalink
feat:#13 发布到Vuepress-生成摘要和读取标签
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 29, 2022
1 parent 6adc1cb commit 613269d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
51 changes: 42 additions & 9 deletions src/components/main/VuepressMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
size="small"
@keyup.enter="tagHandleInputConfirm"
@blur="tagHandleInputConfirm"
autofocus
/>
<el-button v-else class="button-new-tag ml-1 el-tag" size="small" @click="tagShowInput">
{{ $t('main.tag.new') }}
Expand Down Expand Up @@ -107,12 +108,15 @@

<script lang="ts" setup>
import {onBeforeMount, ref} from "vue";
import {getPage, getPageAttrs, getPageId} from "../../lib/siyuan/siyuanUtil";
import {getPage, getPageAttrs, getPageId, getPageMd} from "../../lib/siyuan/siyuanUtil";
import log from "../../lib/logUtil"
import {SIYUAN_PAGE_ATTR_KEY} from "../../constants/siyuanPageConstants"
import {pingyinSlugify, zhSlugify} from "../../lib/util";
import {pingyinSlugify, zhSlugify, formatNumToZhDate} from "../../lib/util";
import {useI18n} from "vue-i18n";
import {ElMessage} from "element-plus";
import {CONSTANTS} from "../../constants/constants";
import {mdToHtml, parseHtml} from "../../lib/htmlUtil";
import {nextTick} from 'vue'
const {t} = useI18n()
Expand All @@ -121,18 +125,20 @@ const formData = ref({
title: "",
customSlug: "",
desc: "",
created: new Date(),
created: "",
checkList: [],
tag: {
inputValue: "",
dynamicTags: [],
dynamicTags: <string[]>([]),
inputVisible: false
},
categories: ["默认分类"]
})
const siyuanData = ref({
pageId: "",
meta: {}
meta: {
tags: ""
}
})
const vuepressData = {
yamlObj: {
Expand Down Expand Up @@ -180,6 +186,14 @@ async function initPage() {
formData.value.customSlug = siyuanData.value.meta[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_SLUG_KEY];
// @ts-ignore
formData.value.desc = siyuanData.value.meta[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_DESC_KEY];
formData.value.created = formatNumToZhDate(page.created)
formData.value.tag.dynamicTags = [];
const tagstr = siyuanData.value.meta.tags || ""
const tgarr = tagstr.split(",")
for (let i = 0; i < tgarr.length; i++) {
formData.value.tag.dynamicTags.push(tgarr[i])
}
}
async function makeSlug() {
Expand Down Expand Up @@ -208,20 +222,39 @@ async function makeSlug() {
}
}
const makeDesc = () => {
async function makeDesc() {
const data = await getPageMd(siyuanData.value.pageId);
const md = data.content
let html = mdToHtml(md)
// formData.value.desc = html;
formData.value.desc = parseHtml(html, CONSTANTS.MAX_PREVIEW_LENGTH, true)
}
const createTimeChanged = (val: any) => {
const createTimeChanged = (val: any) => {
log.logInfo("createTimeChanged=>", val)
}
const tagHandleClose = (tag: any) => {
formData.value.tag.dynamicTags.splice(formData.value.tag.dynamicTags.indexOf(tag), 1)
}
// https://stackoverflow.com/questions/64774113/vue-js-3-use-autofocus-on-input-with-ref-inside-a-method
// https://stackoverflow.com/questions/64774113/vue-js-3-use-autofocus-on-input-with-ref-inside-a-method
// https://www.helloworld.net/p/2721375043
const tagRefInput = ref();
const tagShowInput = () => {
formData.value.tag.inputVisible = true
// this.$refs.tagRefInput.focus()
nextTick(() => {
tagRefInput.value.focus();
});
}
const tagHandleInputConfirm = () => {
if (formData.value.tag.inputValue) {
formData.value.tag.dynamicTags.push(formData.value.tag.inputValue)
}
formData.value.tag.inputVisible = false
formData.value.tag.inputValue = ''
}
const fetchTag = () => {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/siyuan/siyuanUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @returns {Promise<string>}
*/
import {getJSONConf, setJSONConf} from "../config";
import {getBlockByID, getBlockAttrs} from "./siYuanApi.js";
import {getBlockByID, getBlockAttrs, exportMdContent} from "./siYuanApi.js";
import log from "../logUtil";
import {getEnv} from "../envUtil";

Expand Down Expand Up @@ -131,4 +131,12 @@ export async function getPage(pageId: string) {
*/
export async function getPageAttrs(pageId: string) {
return await getBlockAttrs(pageId)
}

/**
* 获取页面的Markdown
* @param pageId
*/
export async function getPageMd(pageId: string) {
return await exportMdContent(pageId);
}
2 changes: 1 addition & 1 deletion src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const formatIsoToZhDate = (str: string, isAddTimeZone: boolean) => {
* @param str '20220718142548'
* @returns {string|*}
*/
export const formatNumToZhDate = (str: string) => {
export const formatNumToZhDate = (str: string): string => {
if (!str) {
return "";
}
Expand Down
9 changes: 9 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ app.use(i18n)
// Element-Plus
app.use(ElementPlus)

// Register a global custom directive called `v-focus`
app.directive('focus', {
// When the bound element is mounted into the DOM...
mounted(el) {
// Focus the element
el.focus()
}
})

// 挂载Vue
app.mount('#app')

0 comments on commit 613269d

Please sign in to comment.