Skip to content

Commit

Permalink
Add fill script
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony1060 committed Feb 21, 2022
1 parent af527db commit 3f2837c
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
yarn-error.log
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<!-- had to do this weird formatting because nice formatting leaver uneeded spacing -->
<pre><div align="center">
Hi 👋
Expand Down
4 changes: 2 additions & 2 deletions README_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Welcome to my Github
<b>----------------------------------</b>

<b>------------ Latest blog post ------------</b>
{post_title_line|36}
{post_title_line|36|single}
| ------------------------------------ |
{post_description_lines|36}
{post_description_lines|36|multi}
<b>------------------------------------------</b>
</div>
</pre>
Expand Down
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "antony1060",
"version": "1.0.0",
"main": "src/index.ts",
"repository": "https://github.com/Antony1060/antony1060.git",
"author": "Antony1060 <antoniostignjedec@gmail.com>",
"license": "MIT",
"scripts": {
"fill": "ts-node src/index.ts"
},
"dependencies": {
"@types/node": "^17.0.19",
"axios": "^0.26.0",
"fast-xml-parser": "^4.0.3",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
}
}
92 changes: 92 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import axios from "axios";
import { XMLParser } from "fast-xml-parser";
import fs from "fs/promises"

const TITLE_REGEX = /\{post_title_line\|\d+\|(single|multi)\}/gm;
const DESCRIPTION_REGEX = /\{post_description_lines\|\d+\|(single|multi)\}/gm;

const IN_PATH = "./README_template.md";
const OUT_PATH = "./README.md";

const REPLACE_BORDER = {
start: "| ",
end: " |"
}

type Placeholder = "post_title_line" | "post_description_lines";

type Replacable = {
placeholder: Placeholder,
length: number,
multiLine: boolean
}

type Post = {
title: string,
description: string
}

const parseReplacable = (src: string, regex: RegExp): Replacable[] =>
src.match(regex)?.map(it => {
const [ p, l, m ] = it.slice(1, -1).split("|");
return {
placeholder: p as Placeholder,
length: parseInt(l),
multiLine: m === "multi"
}
}) ?? [];

const replacableToPlaceholder = ({ placeholder, length, multiLine }: Replacable) =>
`{${placeholder}|${length}|${multiLine ? "multi" : "single"}}`

const nString = (n: number, c: string) => Array(n).fill(c).join("")

const centerPad = (text: string, maxLength: number) => {
if(text.length >= maxLength) return text;

const diff = maxLength - text.length;
return nString(Math.floor(diff / 2), " ") + text + nString(Math.ceil(diff / 2), " ");
}

const formatTextWithReplacable = (text: string, replacable: Replacable): string => {
if(!replacable.multiLine)
return REPLACE_BORDER.start + centerPad(text.length > replacable.length ? text.slice(0, replacable.length - 3) + "..." : text, replacable.length) + REPLACE_BORDER.end;

const lines: string[] = text.split(" ").reduce<string[]>((acc, curr) => {
let last = acc.at(-1);
if(!last || (last + curr).length >= replacable.length) acc.push(curr);
else acc[acc.length - 1] = last + " " + curr;

return acc;
}, []);

return lines.map(it => REPLACE_BORDER.start + centerPad(it, replacable.length) + REPLACE_BORDER.end).join("\n");
}

(async () => {
const template = await fs.readFile(IN_PATH, { encoding: "utf-8" });

const titles = parseReplacable(template, TITLE_REGEX);
const descriptions = parseReplacable(template, DESCRIPTION_REGEX);

const rss: string | false = await axios.get("https://antony.cloud/rss.xml").then(res => res.data).catch(() => false);
if(!rss)
return;

// item will not be an array in case there is no posts, this most likely won't ever happen, but if I ever delete all my posts except one, we will have a fail safe lol
const parsed: { rss: { channel: { item: Post | Post[] } } } = new XMLParser().parse(rss);
const posts = Array.isArray(parsed.rss.channel.item) ? parsed.rss.channel.item : [parsed.rss.channel.item]

const latestPost = posts.at(0);
if(!latestPost) return;

let final = template;

for(const title of titles)
final = final.replace(replacableToPlaceholder(title), formatTextWithReplacable(latestPost.title, title));

for(const description of descriptions)
final = final.replace(replacableToPlaceholder(description), formatTextWithReplacable(latestPost.description, description));

await fs.writeFile(OUT_PATH, final, { encoding: "utf-8" });
})()
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"esModuleInterop": true,
"resolveJsonModule": true,
"lib": ["es2020"],
"downlevelIteration": true,
"strict": true,
}
}
128 changes: 128 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@cspotcode/source-map-consumer@0.8.0":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz#33bf4b7b39c178821606f669bbc447a6a629786b"
integrity sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==

"@cspotcode/source-map-support@0.7.0":
version "0.7.0"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz#4789840aa859e46d2f3173727ab707c66bf344f5"
integrity sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==
dependencies:
"@cspotcode/source-map-consumer" "0.8.0"

"@tsconfig/node10@^1.0.7":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.8.tgz#c1e4e80d6f964fbecb3359c43bd48b40f7cadad9"
integrity sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==

"@tsconfig/node12@^1.0.7":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.9.tgz#62c1f6dee2ebd9aead80dc3afa56810e58e1a04c"
integrity sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==

"@tsconfig/node14@^1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.1.tgz#95f2d167ffb9b8d2068b0b235302fafd4df711f2"
integrity sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==

"@tsconfig/node16@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==

"@types/node@^17.0.19":
version "17.0.19"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.19.tgz#726171367f404bfbe8512ba608a09ebad810c7e6"
integrity sha512-PfeQhvcMR4cPFVuYfBN4ifG7p9c+Dlh3yUZR6k+5yQK7wX3gDgVxBly4/WkBRs9x4dmcy1TVl08SY67wwtEvmA==

acorn-walk@^8.1.1:
version "8.2.0"
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1"
integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==

acorn@^8.4.1:
version "8.7.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.0.tgz#90951fde0f8f09df93549481e5fc141445b791cf"
integrity sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

axios@^0.26.0:
version "0.26.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.0.tgz#9a318f1c69ec108f8cd5f3c3d390366635e13928"
integrity sha512-lKoGLMYtHvFrPVt3r+RBMp9nh34N0M8zEfCWqdWZx6phynIEhQqAdydpyBAAG211zlhX9Rgu08cOamy6XjE5Og==
dependencies:
follow-redirects "^1.14.8"

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

fast-xml-parser@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.0.3.tgz#ad1ebfa53a447369634454073f7952f171ed4aba"
integrity sha512-xhQbg3a/EYNHwK0cxIG1nZmVkHX/0tWihamn5pU4Mhd9KEVE2ga8ZJiqEUgB2sApElvAATOdMTLjgqIpvYDUkQ==
dependencies:
strnum "^1.0.5"

follow-redirects@^1.14.8:
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

strnum@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db"
integrity sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==

ts-node@^10.5.0:
version "10.5.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.5.0.tgz#618bef5854c1fbbedf5e31465cbb224a1d524ef9"
integrity sha512-6kEJKwVxAJ35W4akuiysfKwKmjkbYxwQMTBaAxo9KKAx/Yd26mPUyhGz3ji+EsJoAgrLqVsYHNuuYwQe22lbtw==
dependencies:
"@cspotcode/source-map-support" "0.7.0"
"@tsconfig/node10" "^1.0.7"
"@tsconfig/node12" "^1.0.7"
"@tsconfig/node14" "^1.0.0"
"@tsconfig/node16" "^1.0.2"
acorn "^8.4.1"
acorn-walk "^8.1.1"
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
v8-compile-cache-lib "^3.0.0"
yn "3.1.1"

typescript@^4.5.5:
version "4.5.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==

v8-compile-cache-lib@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.0.tgz#0582bcb1c74f3a2ee46487ceecf372e46bce53e8"
integrity sha512-mpSYqfsFvASnSn5qMiwrr4VKfumbPyONLCOPmsR3A6pTY/r0+tSaVbgPWSAIuzbk3lCTa+FForeTiO+wBQGkjA==

yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

0 comments on commit 3f2837c

Please sign in to comment.