Skip to content

Commit 59a4424

Browse files
committed
Update branch in github workflow
1 parent e5133f4 commit 59a4424

File tree

5 files changed

+268
-1
lines changed

5 files changed

+268
-1
lines changed

.github/workflows/gen-toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55
push:
66
branches:
7-
- master
7+
- main
88

99
env:
1010
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

.gitignore

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Cruft
2+
.DS_Store
3+
.idea
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
lerna-debug.log*
12+
.pnpm-debug.log*
13+
14+
# Diagnostic reports (https://nodejs.org/api/report.html)
15+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Directory for instrumented libs generated by jscoverage/JSCover
24+
lib-cov
25+
26+
# Coverage directory used by tools like istanbul
27+
coverage
28+
*.lcov
29+
30+
# nyc test coverage
31+
.nyc_output
32+
33+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
34+
.grunt
35+
36+
# Bower dependency directory (https://bower.io/)
37+
bower_components
38+
39+
# node-waf configuration
40+
.lock-wscript
41+
42+
# Compiled binary addons (https://nodejs.org/api/addons.html)
43+
build/Release
44+
45+
# Dependency directories
46+
node_modules/
47+
jspm_packages/
48+
49+
# Snowpack dependency directory (https://snowpack.dev/)
50+
web_modules/
51+
52+
# TypeScript cache
53+
*.tsbuildinfo
54+
55+
# Optional npm cache directory
56+
.npm
57+
58+
# Optional eslint cache
59+
.eslintcache
60+
61+
# Optional stylelint cache
62+
.stylelintcache
63+
64+
# Microbundle cache
65+
.rpt2_cache/
66+
.rts2_cache_cjs/
67+
.rts2_cache_es/
68+
.rts2_cache_umd/
69+
70+
# Optional REPL history
71+
.node_repl_history
72+
73+
# Output of 'npm pack'
74+
*.tgz
75+
76+
# Yarn Integrity file
77+
.yarn-integrity
78+
79+
# dotenv environment variable files
80+
.env
81+
.env.development.local
82+
.env.test.local
83+
.env.production.local
84+
.env.local
85+
86+
# parcel-bundler cache (https://parceljs.org/)
87+
.cache
88+
.parcel-cache
89+
90+
# Next.js build output
91+
.next
92+
out
93+
94+
# Nuxt.js build / generate output
95+
.nuxt
96+
dist
97+
98+
# Gatsby files
99+
.cache/
100+
# Comment in the public line in if your project uses Gatsby and not Next.js
101+
# https://nextjs.org/blog/next-9-1#public-directory-support
102+
# public
103+
104+
# vuepress build output
105+
.vuepress/dist
106+
107+
# vuepress v2.x temp and cache directory
108+
.temp
109+
.cache
110+
111+
# Docusaurus cache and generated files
112+
.docusaurus
113+
114+
# Serverless directories
115+
.serverless/
116+
117+
# FuseBox cache
118+
.fusebox/
119+
120+
# DynamoDB Local files
121+
.dynamodb/
122+
123+
# TernJS port file
124+
.tern-port
125+
126+
# Stores VSCode versions used for testing VSCode extensions
127+
.vscode-test
128+
129+
# yarn v2
130+
.yarn/cache
131+
.yarn/unplugged
132+
.yarn/build-state.yml
133+
.yarn/install-state.gz
134+
.pnp.*

package-lock.json

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "java-interview-questions",
3+
"version": "1.0.0",
4+
"description": "Java Interview Questions",
5+
"author": "Sudheer Jonna",
6+
"scripts": {
7+
"gen": "node scripts/toc.mjs"
8+
},
9+
"dependencies": {
10+
"github-slugger": "^2.0.0"
11+
}
12+
}

scripts/toc.mjs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import GitHubSlugger from "github-slugger";
2+
import fs from "fs";
3+
import path, { dirname } from "path";
4+
import { fileURLToPath } from "url";
5+
6+
const slugger = new GitHubSlugger();
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
10+
const filePath = path.join(__dirname, "../README.md");
11+
12+
const TOC_START_MARKER = "<!-- TOC_START -->";
13+
const TOC_END_MARKER = "<!-- TOC_END -->";
14+
const QUESTIONS_START_MARKER = "<!-- QUESTIONS_START -->";
15+
const QUESTIONS_END_MARKER = "<!-- QUESTIONS_END -->";
16+
const QUESTION_TITLE_HEADING_LEVEL = "###";
17+
18+
const rawFile = fs.readFileSync(filePath).toString();
19+
20+
const fileAsLines = rawFile.split("\n");
21+
22+
const tocStartIndex = fileAsLines.findIndex(
23+
(line) => line === TOC_START_MARKER
24+
);
25+
const tocEndIndex = fileAsLines.findIndex((line) => line === TOC_END_MARKER);
26+
27+
const questionsStartIndex = fileAsLines.findIndex(
28+
(line) => line === QUESTIONS_START_MARKER
29+
);
30+
const questionsEndIndex = fileAsLines.findIndex(
31+
(line) => line === QUESTIONS_END_MARKER
32+
);
33+
34+
if (
35+
[tocStartIndex, tocEndIndex, questionsStartIndex, questionsEndIndex].some(
36+
(index) => index === -1
37+
)
38+
) {
39+
throw "One of the crucial indices markers not found";
40+
}
41+
42+
const questions = [];
43+
let currentQuestion = 0;
44+
45+
// Collect the question titles and numbers into an array.
46+
// Also automatically renames the title line if the number is out-of-order.
47+
for (
48+
let lineNumber = questionsStartIndex;
49+
lineNumber < questionsEndIndex;
50+
lineNumber++
51+
) {
52+
const line = fileAsLines[lineNumber];
53+
if (line.includes(` ${QUESTION_TITLE_HEADING_LEVEL} `)) {
54+
currentQuestion++;
55+
const lineParts = line.split(` ${QUESTION_TITLE_HEADING_LEVEL} `);
56+
const questionTitle = lineParts[1];
57+
const questionSlug = slugger.slug(questionTitle);
58+
questions.push({
59+
number: currentQuestion,
60+
title: questionTitle,
61+
slug: questionSlug,
62+
});
63+
64+
fileAsLines[lineNumber] =
65+
currentQuestion + `. ${QUESTION_TITLE_HEADING_LEVEL} ` + questionTitle;
66+
}
67+
}
68+
69+
// Create lines for table of contents using the collected questions.
70+
const tableOfContentsLines = ["| No. | Questions |", "| --- | --------- |"];
71+
72+
questions.forEach(({ number, title, slug }) =>
73+
tableOfContentsLines.push(`| ${number} | [${title}](#${slug}) |`)
74+
);
75+
76+
// Create resulting file and write to file system.
77+
const outputFileLines = [
78+
...fileAsLines.slice(0, tocStartIndex + 1),
79+
...tableOfContentsLines,
80+
...fileAsLines.slice(tocEndIndex),
81+
];
82+
83+
const outputFile = outputFileLines.join("\n");
84+
85+
fs.writeFileSync(filePath, outputFile);
86+
console.info(`Processed ${tableOfContentsLines.length} questions.`);

0 commit comments

Comments
 (0)