-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview-problem.js
More file actions
executable file
·42 lines (36 loc) · 1.26 KB
/
preview-problem.js
File metadata and controls
executable file
·42 lines (36 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env node
// fzf preview script — called by select_problem() in start-leetcode
// Env vars: PROBLEMS_FILE, PROBLEM_ID
const fs = require('fs');
const db = JSON.parse(fs.readFileSync(process.env.PROBLEMS_FILE, 'utf8'));
const p = db.problems.find(x => x.id === process.env.PROBLEM_ID);
if (!p) process.exit(0);
const cleanHtml = html => {
if (!html) return '';
return html
.replace(/<sup>(.*?)<\/sup>/g, '^$1')
.replace(/<sub>(.*?)<\/sub>/g, '_($1)')
.replace(/<code>(.*?)<\/code>/g, '`$1`')
.replace(/<strong>(.*?)<\/strong>/g, '**$1**')
.replace(/<em>(.*?)<\/em>/g, '*$1*')
.replace(/<li>/g, '\n- ')
.replace(/<\/li>/g, '')
.replace(/<\/?(ul|ol|p|div|span|pre|blockquote)[^>]*>/g, '\n')
.replace(/<br\s*\/?>/g, '\n')
.replace(/<[^>]+>/g, '')
.replace(/ /g, ' ')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/\n{3,}/g, '\n\n')
.trim();
};
const desc = cleanHtml(p.description);
console.log('# ' + p.title);
console.log('');
console.log('**Difficulty:** ' + p.difficulty + ' | **Category:** ' + p.category);
console.log('**LeetCode:** ' + p.leetcodeUrl);
console.log('');
console.log(desc.substring(0, 800));