Skip to content

Commit

Permalink
feat: 支持生成摘要
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 16, 2023
1 parent 6bbd3c8 commit 9aa12fa
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"zhi-github-middleware": "^0.2.8",
"zhi-lib-base": "^0.4.4",
"zhi-notion-markdown": "^0.1.4",
"zhi-siyuan-api": "^2.1.6",
"zhi-siyuan-api": "^2.1.7",
"zhi-xmlrpc-middleware": "^0.4.22"
}
}
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions src/components/publish/BatchPublishIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { IPublishCfg } from "~/src/types/IPublishCfg.ts"
import { PageEditMode } from "~/src/models/pageEditMode.ts"
import EditModeSelect from "~/src/components/publish/form/EditModeSelect.vue"
import PublishTime from "~/src/components/publish/form/PublishTime.vue"
import AiSwitch from "~/src/components/publish/form/AiSwitch.vue"
const logger = createAppLogger("publisher-index")
Expand All @@ -65,9 +66,11 @@ const sysKeys = pre.systemCfg.map((item) => {
})
const id = StrUtil.isEmptyString(props.id) ? process.env.VITE_DEV_PAGE_ID : props.id
const formData = reactive({
// loading
isPublishLoading: false,
isDeleteLoading: false,
// process
showProcessResult: false,
errCount: 0,
successBatchResults: <any[]>[],
Expand All @@ -78,8 +81,11 @@ const formData = reactive({
publishCfg: {} as IPublishCfg,
// =========================
// sync attrs start
// extra sync attrs start
// =========================
// AI开关
useAi: false,
// 平台列表
dynList: <string[]>[],
Expand Down Expand Up @@ -201,6 +207,11 @@ const handleForceDelete = async (key: string, id: string, publishCfg: IPublishCf
}
}
const syncAiSwitch = (val: boolean) => {
formData.useAi = val
logger.debug(`syncAiSwitch in batch publish => ${formData.useAi}`)
}
const syncEditMode = (val: PageEditMode) => {
formData.editType = val
logger.debug("syncEditMode in batch publish")
Expand All @@ -211,6 +222,11 @@ const syncDynList = (selectedKeys: string[]) => {
logger.debug("syncDynList in batch publish")
}
const syncDesc = (val: string) => {
formData.siyuanPost.shortDesc = val
logger.debug("syncDesc in batch publish")
}
const syncPublishTime = (val1: Date, val2: Date) => {
formData.siyuanPost.dateCreated = val1
formData.siyuanPost.dateUpdated = val2
Expand Down Expand Up @@ -283,11 +299,23 @@ onMounted(async () => {
<el-divider border-style="dashed" />

<div v-if="formData.editType === PageEditMode.EditMode_complex" class="complex-mode">
<!-- 别名字段 -->
<!-- AI开关 -->
<ai-switch @emitSyncAiSwitch="syncAiSwitch" />

<!-- 别名 -->
<el-form-item :label="t('main.slug')">
<el-input v-model="formData.siyuanPost.wp_slug" :disabled="true" />
</el-form-item>

<!-- 摘要 -->
<publish-description
v-model:use-ai="formData.useAi"
v-model:page-id="id"
v-model:desc="formData.siyuanPost.shortDesc"
v-model:content="formData.siyuanPost.html"
@emitSyncDesc="syncDesc"
/>

<!-- 标签 -->
<publish-tags />

Expand Down
7 changes: 5 additions & 2 deletions src/components/publish/SinglePublishDoPublish.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ const formData = reactive({
},
// =========================
// sync attrs start
// extra sync attrs start
// =========================
// AI开关
useAi: false,
// 页面模式
editType: PageEditMode.EditMode_simple,
// =========================
Expand Down Expand Up @@ -222,7 +225,7 @@ const syncEditMode = (val: PageEditMode) => {
const syncPublishTime = (val1: Date, val2: Date) => {
formData.siyuanPost.dateCreated = val1
formData.siyuanPost.dateUpdated = val2
logger.debug("syncPublishTime in batch publish")
logger.debug("syncPublishTime in single publish")
}
const syncPost = (post: Post) => {
Expand Down
123 changes: 123 additions & 0 deletions src/components/publish/form/PublishDescription.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<!--
- Copyright (c) 2023, Terwer . All rights reserved.
- DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-
- This code is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License version 2 only, as
- published by the Free Software Foundation. Terwer designates this
- particular file as subject to the "Classpath" exception as provided
- by Terwer in the LICENSE file that accompanied this code.
-
- This code is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- version 2 for more details (a copy is included in the LICENSE file that
- accompanied this code).
-
- You should have received a copy of the GNU General Public License version
- 2 along with this work; if not, write to the Free Software Foundation,
- Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-
- Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
- or visit www.terwer.space if you need additional information or have any
- questions.
-->

<script setup lang="ts">
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { reactive, watch } from "vue"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { ElMessage } from "element-plus"
import { HtmlUtil } from "zhi-common"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
const logger = createAppLogger("publish-description")
const { t } = useVueI18n()
const { kernelApi } = useSiyuanApi()
const props = defineProps({
useAi: {
type: Boolean,
default: false,
},
pageId: {
type: String,
default: "",
},
desc: {
type: String,
default: "",
},
content: {
type: String,
default: "",
},
})
const MAX_PREVIEW_LENGTH = 255
const formData = reactive({
isDescLoading: false,
useAi: props.useAi,
pageId: props.pageId,
desc: props.desc,
html: props.content,
})
// 对于未强制刷新组件的情况下需要watch或者computed
watch(
() => props.useAi,
(newValue) => {
formData.useAi = newValue
}
)
const emit = defineEmits(["emitSyncDesc"])
const handleMakeDesc = async () => {
logger.debug("准备生成摘要...")
formData.isDescLoading = true
try {
if (formData.useAi) {
ElMessage.warning("使用人工智能提取摘要。开发中...")
formData.isDescLoading = false
return
} else {
formData.desc = HtmlUtil.parseHtml(formData.html, MAX_PREVIEW_LENGTH, true)
ElMessage.info(`未开启人工智能,直接截取文章前${MAX_PREVIEW_LENGTH}个字符作为摘要`)
}
// 保存属性到思源
const customAttr = {
memo: formData.desc,
}
await kernelApi.setBlockAttrs(formData.pageId, customAttr)
ElMessage.success(t("main.opt.success"))
} catch (e) {
logger.error(t("main.opt.failure") + "=>", e)
ElMessage.error(t("main.opt.failure") + "=>", e)
}
formData.isDescLoading = false
logger.debug("摘要生成完毕.")
}
const onDescChange = () => {
emit("emitSyncDesc", formData.desc)
}
</script>

<template>
<div class="form-desc">
<el-form-item :label="t('main.desc')">
<el-input v-model="formData.desc" :autosize="{ minRows: 3, maxRows: 16 }" type="textarea" @input="onDescChange" />
</el-form-item>
<el-form-item>
<el-button size="small" :loading="formData.isDescLoading" type="primary" @click="handleMakeDesc">
{{ formData.isDescLoading ? t("main.opt.loading") : t("main.auto.fetch.desc") }}
</el-button>
</el-form-item>
</div>
</template>

<style scoped lang="stylus"></style>

0 comments on commit 9aa12fa

Please sign in to comment.