1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.MarkdownCodeBlocks = void 0;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var MarkdownCodeBlocks;
|
11 | (function (MarkdownCodeBlocks) {
|
12 | MarkdownCodeBlocks.CODE_BLOCK_MARKER = '```';
|
13 | const markdownExtensions = [
|
14 | '.markdown',
|
15 | '.mdown',
|
16 | '.mkdn',
|
17 | '.md',
|
18 | '.mkd',
|
19 | '.mdwn',
|
20 | '.mdtxt',
|
21 | '.mdtext',
|
22 | '.text',
|
23 | '.txt',
|
24 | '.Rmd'
|
25 | ];
|
26 | class MarkdownCodeBlock {
|
27 | constructor(startLine) {
|
28 | this.startLine = startLine;
|
29 | this.code = '';
|
30 | this.endLine = -1;
|
31 | }
|
32 | }
|
33 | MarkdownCodeBlocks.MarkdownCodeBlock = MarkdownCodeBlock;
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function isMarkdown(extension) {
|
41 | return markdownExtensions.indexOf(extension) > -1;
|
42 | }
|
43 | MarkdownCodeBlocks.isMarkdown = isMarkdown;
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function findMarkdownCodeBlocks(text) {
|
52 | if (!text || text === '') {
|
53 | return [];
|
54 | }
|
55 | const lines = text.split('\n');
|
56 | const codeBlocks = [];
|
57 | let currentBlock = null;
|
58 | for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
|
59 | const line = lines[lineIndex];
|
60 | const lineContainsMarker = line.indexOf(MarkdownCodeBlocks.CODE_BLOCK_MARKER) === 0;
|
61 | const constructingBlock = currentBlock != null;
|
62 |
|
63 | if (!lineContainsMarker && !constructingBlock) {
|
64 | continue;
|
65 | }
|
66 |
|
67 | if (!constructingBlock) {
|
68 |
|
69 | currentBlock = new MarkdownCodeBlock(lineIndex);
|
70 |
|
71 | const firstIndex = line.indexOf(MarkdownCodeBlocks.CODE_BLOCK_MARKER);
|
72 | const lastIndex = line.lastIndexOf(MarkdownCodeBlocks.CODE_BLOCK_MARKER);
|
73 | const isSingleLine = firstIndex !== lastIndex;
|
74 | if (isSingleLine) {
|
75 | currentBlock.code = line.substring(firstIndex + MarkdownCodeBlocks.CODE_BLOCK_MARKER.length, lastIndex);
|
76 | currentBlock.endLine = lineIndex;
|
77 | codeBlocks.push(currentBlock);
|
78 | currentBlock = null;
|
79 | }
|
80 | }
|
81 | else if (currentBlock) {
|
82 | if (lineContainsMarker) {
|
83 |
|
84 | currentBlock.endLine = lineIndex - 1;
|
85 | codeBlocks.push(currentBlock);
|
86 | currentBlock = null;
|
87 | }
|
88 | else {
|
89 |
|
90 | currentBlock.code += line + '\n';
|
91 | }
|
92 | }
|
93 | }
|
94 | return codeBlocks;
|
95 | }
|
96 | MarkdownCodeBlocks.findMarkdownCodeBlocks = findMarkdownCodeBlocks;
|
97 | })(MarkdownCodeBlocks || (exports.MarkdownCodeBlocks = MarkdownCodeBlocks = {}));
|
98 |
|
\ | No newline at end of file |