Skip to content

Commit

Permalink
Merge pull request #7178 from SNikhill/refactor/ts-scripts
Browse files Browse the repository at this point in the history
Refactor/ts scripts
  • Loading branch information
pettinarip authored Aug 8, 2022
2 parents 0a5acd2 + 136c4f9 commit b5d591a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 27 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"devDependencies": {
"@netlify/functions": "^1.0.0",
"@types/browser-lang": "^0.1.0",
"@types/github-slugger": "^1.3.0",
"@types/luxon": "^2.3.2",
"@types/mdx-js__react": "^1.5.5",
"@types/node": "^17.0.23",
Expand All @@ -90,6 +91,7 @@
"prettier": "^2.2.1",
"pretty-quick": "^3.1.0",
"react-test-renderer": "^17.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.6.3"
},
"scripts": {
Expand All @@ -100,7 +102,7 @@
"crowdin-clean": "rm -rf .crowdin && mkdir .crowdin",
"crowdin-import": "ts-node src/scripts/crowdin-import.ts",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"generate-heading-ids": "node src/scripts/generate-heading-ids.js",
"generate-heading-ids": "ts-node --esm src/scripts/generateHeadingIds.mts",
"start": "gatsby develop",
"start:lambda": "netlify-lambda serve src/lambda",
"start:static": "gatsby build && gatsby serve",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs")
const GitHubSlugger = require("github-slugger")
import fs from "fs"
import BananaSlug from "github-slugger"

// Given a directory (e.g. `pt`), this script adds custom heading IDs
// within all markdown files of the directory (only if one does not exist).
Expand All @@ -13,16 +13,23 @@ const GitHubSlugger = require("github-slugger")

// TODO we should auto-run this script when new markdown files are added to the project

const toc = {}
const headersToChange: Record<
string,
{
text: string
slug: string
}
> = {}

let curLevel = [0, 0, 0]
let curMaxLevel = 1

const walk = (dir, doc) => {
let results = []
const list = fs.readdirSync(dir)
list.forEach(function (file) {
file = dir + "/" + file
//TODO: Figure out what `doc` is and rename it to something better
const walk = (directoryPath: string, doc: string | null) => {
let results: Array<string> = []
const directoryContents: Array<string> = fs.readdirSync(directoryPath)
directoryContents.forEach(function (file) {
file = directoryPath + "/" + file
const stat = fs.statSync(file)
if (stat && stat.isDirectory()) {
// Recurse into a subdirectory
Expand All @@ -41,11 +48,15 @@ const walk = (dir, doc) => {
return results
}

const stripLinks = (line) => {
return line.replace(/\[([^\]]+)\]\([^)]+\)/, (match, p1) => p1)
const stripLinks = (line: string): string => {
return line.replace(/\[([^\]]+)\]\([^)]+\)/, (match, p1: string) => p1)
}

const addHeaderID = (line, slugger, write = false) => {
const addHeaderID = (
line: string,
slugger: BananaSlug,
write = false
): string | undefined => {
// check if we're a header at all
if (!line.startsWith("#")) {
return line
Expand Down Expand Up @@ -73,12 +84,12 @@ const addHeaderID = (line, slugger, write = false) => {
}
curMaxLevel = 1
const headerNumber = curLevel.join(".")
let slug = null
let slug = ""
if (!write) {
// const match = /^.+(\s*\{#([A-Za-z0-9\-_]+?)\}\s*)$/.exec(line);
// slug = match ? match[2].toLowerCase() : slugger.slug(stripLinks(headingText));
slug = slugger.slug(stripLinks(headingText))
toc[headerNumber] = {
headersToChange[headerNumber] = {
text: headingText,
slug,
}
Expand All @@ -87,8 +98,8 @@ const addHeaderID = (line, slugger, write = false) => {
// if (curLevel[1] > 0)
// console.log(` ${curLevel[1]}. [${title}](#${slug})`);
} else {
if (headerNumber in toc) {
slug = toc[headerNumber].slug
if (headerNumber in headersToChange) {
slug = headersToChange[headerNumber].slug
console.log("\twrite heading ID", headerNumber, headingText, "==>", slug)
return `${headingLevel} ${headingText} {#${slug}}`
} else {
Expand All @@ -104,11 +115,11 @@ const addHeaderID = (line, slugger, write = false) => {
}
}

const addHeaderIDs = (lines, write = false) => {
const addHeaderIDs = (lines: Array<string>, write = false): Array<string> => {
// Sluggers should be per file
const slugger = new GitHubSlugger()
const slugger = new BananaSlug()
let inCode = false
const results = []
const results: Array<string> = []
lines.forEach((line) => {
// Ignore code blocks
if (line.startsWith("```")) {
Expand All @@ -121,12 +132,23 @@ const addHeaderIDs = (lines, write = false) => {
return
}

results.push(addHeaderID(line, slugger, write))
const headerTextWithSlug = addHeaderID(line, slugger, write)
if (headerTextWithSlug) {
results.push(headerTextWithSlug)
}
})
return results
}

const traverseHeaders = (path, doc = "", write = false) => {
type TraverseHeadersFunction = (
path: string,
config: { doc?: string; write?: boolean }
) => void

const traverseHeaders: TraverseHeadersFunction = (
path,
{ doc = "", write = false }
) => {
const files = walk(path, doc)
files.forEach((file) => {
if (!file.endsWith(".md")) {
Expand All @@ -135,27 +157,31 @@ const traverseHeaders = (path, doc = "", write = false) => {

console.log(`>>> processing ${file}`)
curLevel = [0, 0, 0]
const content = fs.readFileSync(file, "utf8")
const lines = content.split("\n")
const content: string = fs.readFileSync(file, "utf8")
const lines: Array<string> = content.split("\n")
const updatedLines = addHeaderIDs(lines, write)
if (write) {
fs.writeFileSync(file, updatedLines.join("\n"))
}
})
if (!write) {
console.log(toc)
console.log(headersToChange)
}
}

const addHeaderIDsForDir = (path) => {
const addHeaderIDsForDir = (path: string) => {
if (path.includes("translations")) {
throw new Error(`Heading ID generation is intended for English files only.`)
}
const fullPath = `src/content/${path}`
traverseHeaders(fullPath, null, false)
traverseHeaders(fullPath, null, true)
traverseHeaders(fullPath, { write: false })
traverseHeaders(fullPath, { write: true })
}

const [path] = process.argv.slice(2)

if (!path) {
throw new Error("No Valid Path Provided")
}

addHeaderIDsForDir(path)
69 changes: 69 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,13 @@
resolved "https://registry.yarnpkg.com/@builder.io/partytown/-/partytown-0.5.4.tgz#1a89069978734e132fa4a59414ddb64e4b94fde7"
integrity sha512-qnikpQgi30AS01aFlNQV6l8/qdZIcP76mp90ti+u4rucXHsn4afSKivQXApqxvrQG9+Ibv45STyvHizvxef/7A==

"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1"
integrity sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@emotion/cache@^11.4.0", "@emotion/cache@^11.7.1":
version "11.7.1"
resolved "https://registry.yarnpkg.com/@emotion/cache/-/cache-11.7.1.tgz#08d080e396a42e0037848214e8aa7bf879065539"
Expand Down Expand Up @@ -2956,6 +2963,14 @@
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.13.tgz#b6461fb0c2964356c469e115f504c95ad97ab88c"
integrity sha512-GryiOJmNcWbovBxTfZSF71V/mXbgcV3MewDe3kIMCLyIh5e7SKAeUZs+rMnJ8jkMolZ/4/VsdBmMrw3l+VdZ3w==

"@jridgewell/[email protected]":
version "0.3.9"
resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9"
integrity sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==
dependencies:
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@jridgewell/trace-mapping@^0.3.9":
version "0.3.13"
resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.13.tgz#dcfe3e95f224c8fe97a87a5235defec999aa92ea"
Expand Down Expand Up @@ -3569,6 +3584,26 @@
resolved "https://registry.yarnpkg.com/@trysound/sax/-/sax-0.2.0.tgz#cccaab758af56761eb7bf37af6f03f326dd798ad"
integrity sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==

"@tsconfig/node10@^1.0.7":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.9.tgz#df4907fc07a886922637b15e02d4cebc4c0021b2"
integrity sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==

"@tsconfig/node12@^1.0.7":
version "1.0.11"
resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.11.tgz#ee3def1f27d9ed66dac6e46a295cffb0152e058d"
integrity sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==

"@tsconfig/node14@^1.0.0":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.3.tgz#e4386316284f00b98435bf40f72f75a09dabf6c1"
integrity sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==

"@tsconfig/node16@^1.0.2":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.3.tgz#472eaab5f15c1ffdd7f8628bd4c4f753995ec79e"
integrity sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==

"@turist/fetch@^7.1.7":
version "7.1.7"
resolved "https://registry.yarnpkg.com/@turist/fetch/-/fetch-7.1.7.tgz#a2b1f7ec0265e6fe0946c51eef34bad9b9efc865"
Expand Down Expand Up @@ -3660,6 +3695,11 @@
resolved "https://registry.yarnpkg.com/@types/get-port/-/get-port-3.2.0.tgz#f9e0a11443cc21336470185eae3dfba4495d29bc"
integrity sha512-TiNg8R1kjDde5Pub9F9vCwZA/BNW9HeXP5b9j7Qucqncy/McfPZ6xze/EyBdXS5FhMIGN6Fx3vg75l5KHy3V1Q==

"@types/github-slugger@^1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@types/github-slugger/-/github-slugger-1.3.0.tgz#16ab393b30d8ae2a111ac748a015ac05a1fc5524"
integrity sha512-J/rMZa7RqiH/rT29TEVZO4nBoDP9XJOjnbbIofg7GQKs4JIduEO3WLpte+6WeUz/TcrXKlY+bM7FYrp8yFB+3g==

"@types/glob@*":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
Expand Down Expand Up @@ -4346,6 +4386,11 @@ acorn-jsx@^5.3.1:
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"
integrity sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==

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@^6.4.1:
version "6.4.2"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.4.2.tgz#35866fd710528e92de10cf06016498e47e39e1e6"
Expand Down Expand Up @@ -15751,6 +15796,25 @@ ts-invariant@^0.9.4:
dependencies:
tslib "^2.1.0"

ts-node@^10.9.1:
version "10.9.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b"
integrity sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==
dependencies:
"@cspotcode/source-map-support" "^0.8.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.1"
yn "3.1.1"

ts-node@^9:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
Expand Down Expand Up @@ -16378,6 +16442,11 @@ uuid@^8.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==

v8-compile-cache@^2.0.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"
Expand Down

0 comments on commit b5d591a

Please sign in to comment.