Skip to content

Commit

Permalink
🎨 replace js to python for build posts
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed Jan 21, 2025
1 parent 46ddcf3 commit 3978518
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 52 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ jobs:
steps:
- name: Check out repo
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- name: Set up Node.js
uses: actions/setup-node@v3
with:
python-version: 3.8
node-version: '18'
- uses: actions/cache@v2
name: Configure pip caching
name: Configure npm caching
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install Python dependencies
${{ runner.os }}-node-
- name: Install Node.js dependencies
run: |
python -m pip install -r requirements.txt
npm install
- name: Update README
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |-
python build.py
node build.js
cat README.md
- name: Commit and push if changed
run: |-
Expand Down
53 changes: 53 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const fs = require('fs').promises;
const path = require('path');
const axios = require('axios');

async function fetchCiTime(filePath) {
const url = `https://api.github.com/repos/tw93/weekly/commits?path=${filePath}&page=1&per_page=1`;
const response = await axios.get(url);
const ciTime = response.data[0].commit.committer.date.split('T')[0];
return ciTime;
}

async function main() {
const readmeContent = "# 潮流周刊\n\n> 记录工程师 Tw93 的不枯燥生活,欢迎订阅,也欢迎 [推荐](https://github.com/tw93/weekly/discussions/22) 你的好东西,Fork 自用可见 [开发文档](https://github.com/tw93/weekly/blob/main/Deploy.md),期待你玩得开心~\n\n";

const files = await fs.readdir('./src/pages/posts');
const mdFiles = files
.filter(file => file.endsWith('.md'))
.sort((a, b) => {
const numA = parseInt(a.match(/(\d+)/)[0]);
const numB = parseInt(b.match(/(\d+)/)[0]);
return numB - numA;
});

const posts = [];
let recentContent = '';
let readmeContent2 = '';

for (let i = 0; i < mdFiles.length; i++) {
const name = mdFiles[i];
const filePath = encodeURIComponent(name);
const oldTitle = name.split('.md')[0];
const num = parseInt(oldTitle.split('-')[0]);
const shortTitle = oldTitle.split('-')[1];
const url = `https://weekly.tw93.fun/posts/${oldTitle}`;
const title = `第 ${num} 期 - ${shortTitle}`;

posts.push({ num, title: shortTitle, url });
readmeContent2 += `* [${title}](${url})\n`;

if (i < 5) {
const modified = await fetchCiTime(`/src/pages/posts/${filePath}`);
recentContent += `* [${title}](${url}) - ${modified}\n`;
}
}

await Promise.all([
fs.writeFile('README.md', readmeContent + readmeContent2),
fs.writeFile('RECENT.md', recentContent),
fs.writeFile('posts.json', JSON.stringify(posts, null, 2))
]);
}

main().catch(console.error);
39 changes: 0 additions & 39 deletions build.py

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@pagefind/default-ui": "^1.3.0",
"astro": "^4.16.18",
"axios": "^1.7.9",
"dayjs": "^1.11.13",
"lozad": "^1.16.0",
"motion": "^10.18.0",
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
python-graphql-client==0.3.0
httpx==0.23.3
datetime

0 comments on commit 3978518

Please sign in to comment.