Skip to content

Commit

Permalink
🛠️新增文档质量分数
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcaffebabe committed May 18, 2022
1 parent 29dcc64 commit 3290373
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 253 deletions.
70 changes: 51 additions & 19 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build-pdf": "ts-node ./src/scripts/buildPdf.ts",
"back-origin": "ts-node ./src/trigger/BackOriginTrigger.ts",
"similar-detect": "ts-node ./src/scripts/similarDetect.ts",
"doc-cluster": "ts-node ./src/scripts/generateDocCluster.ts"
"doc-cluster": "ts-node ./src/scripts/generateDocCluster.ts",
"test": "ts-node -r tsconfig-paths/register --files %npm_config_file%"
},
"dependencies": {
"@element-plus/icons-vue": "1.1.4",
Expand Down Expand Up @@ -58,6 +59,7 @@
"rollup-plugin-visualizer": "5.5.4",
"simple-git": "3.1.1",
"ts-node": "10.4.0",
"tsconfig-paths": "^4.0.0",
"typescript": "4.4.3",
"vite": "2.6.0",
"vite-plugin-prismjs": "0.0.8",
Expand Down
6 changes: 6 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CommitInfo from '@/dto/CommitInfo'
import YuequeDraft from '@/dto/YuqueDraft'
import Category from '@/dto/Category'
import ClusterNode from '@/dto/ClusterNode'
import DocQuality from '@/dto/doc/DocQuality'
// import DocService from '@/service/DocService'

const baseUrl = () => {
Expand Down Expand Up @@ -146,6 +147,11 @@ class Api implements Cacheable{
return this.requestDataUseJsDelivr(UrlConst.commitTotalTrend)
}

@cache
public async getDocQualityData(): Promise<DocQuality[]> {
return (await axios.get(UrlUtils.concatUrl(baseUrl(), UrlConst.docQualityJson))).data
}

/**
*
* 获取instapaper原始html数据
Expand Down
55 changes: 55 additions & 0 deletions src/build/DocService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import ArrayUtils from "../util/ArrayUtils";
import CommitInfo from "@/dto/CommitInfo";
import ClusterNode from "@/dto/ClusterNode";
import axios from "axios";
import DocQuality from "../dto/doc/DocQuality";
const proxy = require("node-global-proxy").default;
import Cache from "../decorator/Cache";

const cache = Cache()


class DocService extends BaseService implements Cacheable {
Expand Down Expand Up @@ -129,6 +133,7 @@ class DocService extends BaseService implements Cacheable {
* @return {*} {Promise<KnowledgeNode>}
* @memberof DocService
*/
@cache
public async getKnowledgeNode(path: string): Promise<KnowledgeNode> {
return fs.promises.readFile(path).then(buffer => {
const md = buffer.toString();
Expand Down Expand Up @@ -185,6 +190,7 @@ class DocService extends BaseService implements Cacheable {
* @return {*} {Promise<KnowledgeNode[]>}
* @memberof DocService
*/
@cache
public async generateKnowledgeNetwork(): Promise<KnowledgeNode[]>{
const ignoreDoc = ['README', 'SUMMARY'];
const fileList = BaseService.listFilesBySuffix('md', 'doc')
Expand All @@ -196,6 +202,55 @@ class DocService extends BaseService implements Cacheable {
}


/**
*
* 生成所有文档的质量分数
* @return {*} {Promise<DocQuality[]>}
* @memberof DocService
*/
@cache
public async generateDocQualityData(): Promise<DocQuality[]> {
const knowledgeNodes = await this.generateKnowledgeNetwork()

// 每个文档的外链数 外部 -> 自己
const outLinksMap = new Map<string, number>()
// 每个文档的内链数 自己 -> 外部
const inLinksMap= new Map<string, number>()
for(let node of knowledgeNodes) {
if (!inLinksMap.has(node.id)) {
inLinksMap.set(node.id, node.links?.length || 0)
}
for(let child of node.links || []) {
outLinksMap.set(node.id, 1 + (outLinksMap.get(node.id) || 0))
}
}

const ignoreDoc = ['README', 'SUMMARY'];
const fileList = BaseService.listFilesBySuffix('md', 'doc')
const taskList :Promise<DocQuality>[] = []
const that = this
async function task(file: string): Promise<DocQuality> {
const id = DocUtils.docUrl2Id(file)
/*
文档质量 = 每千字数 + 外链数 + 内链数
*/
const docQuality = new DocQuality()
docQuality.id = id
const md = (await fs.promises.readFile(file)).toString()
docQuality.quality = that.cleanText(md).length / 1000 + (outLinksMap.get(id) || 0) + (inLinksMap.get(id) || 0)
docQuality.segementQuality = that.md2TextSegement(md).map(v => v.txt.length / 1000)
return docQuality
}
for(let file of fileList) {
taskList.push(task(file))
}
return (await Promise.all(taskList))
.filter(v => v.id.indexOf("leetcode") == -1)
.filter(v => ignoreDoc.indexOf(v.id) == -1)
.sort((a, b) => b.totalQuality - a.totalQuality)
}


/**
*
* 生成一份根据最后提交时间正序排序的文档列表
Expand Down
1 change: 1 addition & 0 deletions src/const/UrlConst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export default {
tagMappingJson: '/tagMapping.json',
wordcloudJson: '/wordcloud.json',
docClusterJson: '/docCluster.json',
docQualityJson: '/docQuality.json',
}
2 changes: 1 addition & 1 deletion src/decorator/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CacheService from '@/service/CacheService'
import CacheService from '../service/CacheService'
import Cacheable from './Cacheable';
const cacheService = CacheService.getInstance()

Expand Down
8 changes: 8 additions & 0 deletions src/dto/doc/DocQuality.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default class DocQuality {
id: string = ''
quality: number = 0
segementQuality: number[] = []
get totalQuality(): number {
return this.quality + this.segementQuality.reduce((a, b) => a+b, 0)
}
}
18 changes: 16 additions & 2 deletions src/pages/doc/DocPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<template #default>
<div class="main-content">
<doc-breadcrumb-nav />
<p class="create-time">⏰创建时间: {{new Date(file.createTime).toLocaleString()}}</p>
<p class="create-time">⏰<span>创建时间: </span>{{new Date(file.createTime).toLocaleString()}}</p>
<p class="quality-score">⚽<span>质量分数: </span>{{quality}}</p>
<tag-list :tags="file.formattedMetadata.tags"/>
<!-- doc主体开始 -->
<div class="markdown-section" ref="markdownSection" :class="{'center': showAside}" v-html="contentHtml" :style="{'width': isDrawerShow ? '960px': '74%'}"></div>
Expand Down Expand Up @@ -200,6 +201,9 @@ export default defineComponent({
contentHtml(): string {
return DocService.renderMd(this.file);
},
quality(): string {
return DocService.calcQuanlityStr(this.file.id);
}
},
methods: {
docUrl2Id(url: string) {
Expand Down Expand Up @@ -351,7 +355,17 @@ export default defineComponent({
padding-top: 10px;
margin: 0;
font-size: 12px;
color: #888;
span {
color: #888;
}
}
.quality-score {
padding: 2px;
margin: 0;
font-size: 12px;
span {
color: #888;
}
}
.toc-wrapper {
transition: all 0.2s;
Expand Down
12 changes: 10 additions & 2 deletions src/pages/doc/category/CategoryItemContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
<el-link :href="'/doc/' + docId" @click.prevent="$router.push('/doc/' + docId)" type="primary">{{ docId }}</el-link>
</p>
<el-badge
:value="lastPastDays + '天前更新'"
:value="'⏰' + lastPastDays + '天前更新'"
class="item"
:type="calcUpdateType(lastPastDays)"
></el-badge>
<el-badge
:value="wordCount + '字'"
:value="'✏️' + wordCount + '字'"
class="item"
type="primary"
></el-badge>
<el-badge
:value="'⚽' + quality"
class="item"
type="warning"
></el-badge>
<tag-list :tags="tags" />
<el-alert :closable="false" v-if="docMetadata.books && docMetadata.books.length > 0">
<template #default>
Expand Down Expand Up @@ -73,6 +78,9 @@ export default defineComponent({
},
docId(){
return DocUtils.docUrl2Id(this.categoryLink);
},
quality() {
return DocService.calcQuanlityStr(this.docId)
}
},
methods: {
Expand Down
Loading

0 comments on commit 3290373

Please sign in to comment.