-
Notifications
You must be signed in to change notification settings - Fork 0
/
erasmus.ts
39 lines (32 loc) · 1.07 KB
/
erasmus.ts
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
import { Transform, TransformOptions } from 'stream';
export type CodeBlocks = string[];
/**
* Extracts code blocks from a Markdown document.
*/
export function extractCode(markdown: string): CodeBlocks {
const regex = /^`{3}\w*$/gmu;
return markdown
.split(regex)
/* After splitting the markdown content at each occurrence of a code block fence
(```[language]), every second element of the resulting array contains a chunk of code. */
.filter((_, index) => index % 2 > 0)
.map((item) => item.trim());
}
/**
* Transform stream extracting code blocks from a readable stream of Markdown text.
*/
export class CodeExtractor extends Transform {
private markdown: string;
constructor(options: TransformOptions) {
super(options);
this.markdown = ``;
}
_transform(chunk: string, _: string, callback: Function): void {
this.markdown += chunk;
callback();
}
_flush(callback: Function): void {
this.push(`${extractCode(this.markdown).join(`\n`)}\n`);
callback();
}
}