Skip to content

Commit

Permalink
feat(index.md): 自动同步掘金文章脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
lv-z-l committed Aug 15, 2023
1 parent 38c4c3d commit 12552d7
Show file tree
Hide file tree
Showing 10 changed files with 1,180 additions and 689 deletions.
16 changes: 5 additions & 11 deletions .vitepress/cache/deps/_metadata.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
{
"hash": "5fda2f0e",
"browserHash": "dc2fbd3d",
"hash": "855f32fe",
"browserHash": "7703e6a4",
"optimized": {
"vue": {
"src": "../../../node_modules/.pnpm/[email protected]/node_modules/vue/dist/vue.runtime.esm-bundler.js",
"file": "vue.js",
"fileHash": "3910e4dd",
"fileHash": "df6bee7c",
"needsInterop": false
},
"vitepress > @vue/devtools-api": {
"src": "../../../node_modules/.pnpm/@[email protected]/node_modules/@vue/devtools-api/lib/esm/index.js",
"file": "vitepress___@vue_devtools-api.js",
"fileHash": "68eb41cc",
"fileHash": "a49b330b",
"needsInterop": false
},
"@theme/index": {
"src": "../../../node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/vitepress/dist/client/theme-default/index.js",
"file": "@theme_index.js",
"fileHash": "dca0976b",
"needsInterop": false
},
"xmind-embed-viewer": {
"src": "../../../node_modules/.pnpm/[email protected]/node_modules/xmind-embed-viewer/dist/esm/index.js",
"file": "xmind-embed-viewer.js",
"fileHash": "76a6495e",
"fileHash": "a0d89c06",
"needsInterop": false
}
},
Expand Down
6 changes: 2 additions & 4 deletions .vitepress/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'vitepress'
import { genNavSide } from './dym-import'
import { genNavSide } from '../dym-import'

const path = require('path')

Expand All @@ -25,8 +25,6 @@ export default defineConfig({
nav,
sidebar,
logo: 'https://p3-passport.byteimg.com/img/user-avatar/464e00b478bc3c604aad6b2518137d5a~180x180.awebp',
socialLinks: [
{ icon: 'github', link: 'https://github.com/lv-z-l/front-end-blog' },
],
socialLinks: [{ icon: 'github', link: 'https://github.com/lv-z-l/front-end-blog' }],
},
})
2 changes: 1 addition & 1 deletion articles/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ hero:
<script setup>
import { reactive, ref } from 'vue'
import Rail from '@/Rail'
import { origin, tagColors } from '../.vitepress/timelines'
import { origin, tagColors } from './timelines'
import cat from '../.vitepress/public/assets/astrocat.png'
import drone from '../.vitepress/public/assets/hero-drone.webp'
import TimeLineItem from '@/TimeLineItem'
Expand Down
322 changes: 128 additions & 194 deletions .vitepress/timelines.js → articles/juejin-articles.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions articles/other/packagemanager.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: 聊聊 npm & yarn & pnpm 包管理机制
title: npm & yarn & pnpm 包管理机制
author: lvzl
---
<img src="https://mp-d22f2f25-96ec-4381-920f-a0d8df227b60.cdn.bspapp.com/cloudstorage/0112a686-55e0-4e6d-a164-b79c45f17453.jpg" />

<img src="https://mp-d22f2f25-96ec-4381-920f-a0d8df227b60.cdn.bspapp.com/cloudstorage/0112a686-55e0-4e6d-a164-b79c45f17453.jpg" />
29 changes: 29 additions & 0 deletions articles/timelines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { origin } from './juejin-articles'

const alltags = []

for (let i = 1; i < origin.length; i++) {
const { tags } = origin[i]
tags && alltags.push(...tags.split('、'))
}

function getAnyColor() {
const str = '456789abcdef'
return (
'#' +
new Array(6)
.fill('1')
.map(() => str[Math.floor(Math.random() * 12)])
.join('')
)
}

const tagColors = {}

const filterTags = [...new Set(alltags)].filter(tag => !tag.startsWith('掘金'))

for (const tag of filterTags) {
tagColors[tag] = getAnyColor()
}

export { origin, tagColors }
65 changes: 65 additions & 0 deletions buildstart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { writeFileSync } = require('fs')

const path = require('path')

let fetch
import('node-fetch').then(({ default: f }) => {
fetch = f
reqJuejin(1)
})

const articles = []

const reqJuejin = page_no =>
fetch('https://api.juejin.cn/content_api/v1/article/list_by_user?aid=2608&uuid=7259181137129358863&spider=0', {
headers: {
accept: '*/*',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'content-type': 'application/json',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'x-secsdk-csrf-token': '00010000000149c988447e760ec6d5e15e95b745171c2593a9775d684f3677613381fc35de11177b836c7b8fff5d',
cookie:
'__tea_cookie_tokens_2608=%257B%2522web_id%2522%253A%25227259181137129358863%2522%252C%2522user_unique_id%2522%253A%25227259181137129358863%2522%252C%2522timestamp%2522%253A1690159839194%257D; passport_csrf_token=25e655362356da605beb01f738726370; passport_csrf_token_default=25e655362356da605beb01f738726370; n_mh=pKG77FJQOrZTAh4MhH3pUvSrlLCo6Ssg99ix1gJThGM; passport_auth_status=26315e7580b2ce148a025296ffd95b23%2C; passport_auth_status_ss=26315e7580b2ce148a025296ffd95b23%2C; sid_guard=f9c1daab88dd442a9a3562841a271d51%7C1690159898%7C31536000%7CTue%2C+23-Jul-2024+00%3A51%3A38+GMT; uid_tt=56e8e1e993a03973408cb87e9e9028e1; uid_tt_ss=56e8e1e993a03973408cb87e9e9028e1; sid_tt=f9c1daab88dd442a9a3562841a271d51; sessionid=f9c1daab88dd442a9a3562841a271d51; sessionid_ss=f9c1daab88dd442a9a3562841a271d51; sid_ucp_v1=1.0.0-KDhiYzcxN2RhYWYyZGZiOGY3MDJmYmZiMGI0MTU3NjUzMTI3NDc1ODYKFwiOqLCJ_PXvARCalvelBhiwFDgCQPEHGgJscSIgZjljMWRhYWI4OGRkNDQyYTlhMzU2Mjg0MWEyNzFkNTE; ssid_ucp_v1=1.0.0-KDhiYzcxN2RhYWYyZGZiOGY3MDJmYmZiMGI0MTU3NjUzMTI3NDc1ODYKFwiOqLCJ_PXvARCalvelBhiwFDgCQPEHGgJscSIgZjljMWRhYWI4OGRkNDQyYTlhMzU2Mjg0MWEyNzFkNTE; store-region=cn-zj; store-region-src=uid; _ga=GA1.2.143415306.1690159915; _tea_utm_cache_2608={%22utm_source%22:%22news%22%2C%22utm_medium%22:%22push%22%2C%22utm_campaign%22:%22jinshi20235%22}; _ga_S695FMNGPJ=GS1.2.1691734415.12.0.1691734415.60.0.0; csrf_session_id=348b6529aba6ec35bc005002b4b6e1da; msToken=_MdCk4YIf2IAhHtONBc5dd2ZvdmPGnEnWIb7WIdFPAA0Gl0k_S29X64_mCE0MVl71UCSNL-fuCV9WZnkfGXwhvZZrGWGqe53adDdtpWNg6oHY-OovTiCf881ZcVP31Xk',
Referer: 'https://juejin.cn/',
'Referrer-Policy': 'strict-origin-when-cross-origin',
},
body: '{"audit_status":null,"keyword":"","page_size":20,"page_no": ' + page_no + '}',
method: 'POST',
})
.then(res => res.json())
.then(jsonData => {
const { count, data } = jsonData
articles.push(...data)
if (count / 20 > page_no) {
reqJuejin(page_no + 1)
} else {
getTimeLines()
}
})

/**
* 从掘金的接口获取数据并处理返回
* @param {*} articles
* @returns
*/
function getTimeLines() {
const timelines = articles.map(arl => {
const { article_info, tags, article_id } = arl
const { title, ctime, brief_content, cover_image } = article_info
return {
article_id,
title,
content: brief_content,
cover_image,
tags: tags.map(tag => tag.tag_name).join('、'),
time: new Date(Number(ctime.padEnd(ctime.length + 3, '0'))).toLocaleDateString('zh-CN'),
}
})

writeFileSync(path.resolve('articles/juejin-articles.js'), `export const origin = ${JSON.stringify(timelines, null, 2)}`)
}
21 changes: 13 additions & 8 deletions .vitepress/dym-import.js → dym-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const path = require('path')

const SRC_DIR = 'articles'

const titlePrefix = 'title: ', titleSuffix = 'author:'
const TITLE_PRE_FIX = 'title: '
const TITLE_SUFFIX = 'author: '

const IGNORE_DIR = ['index', 'nav']

Expand All @@ -17,8 +18,7 @@ function read(path, res) {
} else {
if (!IGNORE_DIR.includes(dir)) {
const fileContent = fs.readFileSync(current)
const text = fileContent.subarray(fileContent.indexOf(titlePrefix), fileContent.indexOf(titleSuffix))
.toString().replace('title: ', '').trim()
const text = fileContent.subarray(fileContent.indexOf(TITLE_PRE_FIX), fileContent.indexOf(TITLE_SUFFIX)).toString().replace('title: ', '').trim()
res.push({ text, link: current.split(SRC_DIR)[1].replace('.md', '') })
}
}
Expand All @@ -40,7 +40,7 @@ function getNavJs() {
}
data.dir = dir
res.push(data)
} catch (error) { }
} catch (error) {}
}
return res
}
Expand All @@ -60,16 +60,21 @@ export function genNavSide() {
const { dir, navText, sideDirs, sideText } = navJs[i]
const navItem = {
text: navText,
link: `/${dir}/`
link: `/${dir}/`,
}
if (sideDirs) {
sidebar[`/${dir}/`] = sideDirs.map((sidedir, index) => ({ text: sideText[index], items: genItems(`${dir}/${sidedir}`), collapsed: true }))
sidebar[`/${dir}/`] = sideDirs.map((sidedir, index) => ({
text: sideText[index],
items: genItems(`${dir}/${sidedir}`),
collapsed: true,
}))
} else {
sidebar[`/${dir}/`] = genItems(dir)
}
nav.push(navItem)
}
return {
nav, sidebar
nav,
sidebar,
}
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"scripts": {
"docs:dev": "vitepress dev",
"docs:build": "vitepress build && node ./buildend/index.js",
"docs:preview": "vitepress preview"
"docs:preview": "vitepress preview",
"sync-juejin-articles": "node ./buildstart/index.js"
},
"devDependencies": {
"cz-conventional-changelog": "^3.3.0",
"vitepress": "1.0.0-beta.5"
"vitepress": "1.0.0-beta.5",
"node-fetch": "^3.3.2"
},
"dependencies": {
"jsdom": "^22.1.0",
Expand Down
Loading

0 comments on commit 12552d7

Please sign in to comment.