Skip to content

Commit f3d5484

Browse files
author
Ryan A. Johnson
committed
docs(util): add util JS for code snippets
1 parent b81abb3 commit f3d5484

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/_util.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function stripIndent (rawString) {
2+
const match = rawString.match(/^[ \t]*(?=\S)/gm);
3+
4+
if (!match) {
5+
return rawString;
6+
}
7+
8+
const minIndent = Math.min.apply(Math, match.map(x => x.length));
9+
10+
const reMinIndent = new RegExp(`^[ \\t]{${minIndent}}`, 'gm');
11+
12+
let strReindented = rawString;
13+
if (minIndent > 0) {
14+
strReindented = rawString.replace(reMinIndent, '');
15+
}
16+
17+
return strReindented.trim();
18+
}//stripIndent()
19+
20+
function snippet (raw) {
21+
// https://regex101.com/r/hKMzZP/4
22+
let normalized = raw.replace(/(\s+^\s+$|\s+(?=>))/gm, '');
23+
return stripIndent(normalized);
24+
}
25+
26+
export default {
27+
snippet,
28+
};

0 commit comments

Comments
 (0)