UNPKG

3.82 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.MarkdownCodeBlocks = void 0;
6/**
7 * The namespace for code block functions which help
8 * in extract code from markdown text
9 */
10var 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 * Check whether the given file extension is a markdown extension
36 * @param extension - A file extension
37 *
38 * @returns true/false depending on whether this is a supported markdown extension
39 */
40 function isMarkdown(extension) {
41 return markdownExtensions.indexOf(extension) > -1;
42 }
43 MarkdownCodeBlocks.isMarkdown = isMarkdown;
44 /**
45 * Construct all code snippets from current text
46 * (this could be potentially optimized if we can cache and detect differences)
47 * @param text - A string to parse codeblocks from
48 *
49 * @returns An array of MarkdownCodeBlocks.
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 // Skip this line if it is not part of any code block and doesn't contain a marker.
63 if (!lineContainsMarker && !constructingBlock) {
64 continue;
65 }
66 // Check if we are already constructing a code block.
67 if (!constructingBlock) {
68 // Start constructing a new code block.
69 currentBlock = new MarkdownCodeBlock(lineIndex);
70 // Check whether this is a single line code block of the form ```a = 10```.
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 // End of block, finish it up.
84 currentBlock.endLine = lineIndex - 1;
85 codeBlocks.push(currentBlock);
86 currentBlock = null;
87 }
88 else {
89 // Append the current line.
90 currentBlock.code += line + '\n';
91 }
92 }
93 }
94 return codeBlocks;
95 }
96 MarkdownCodeBlocks.findMarkdownCodeBlocks = findMarkdownCodeBlocks;
97})(MarkdownCodeBlocks = exports.MarkdownCodeBlocks || (exports.MarkdownCodeBlocks = {}));
98//# sourceMappingURL=markdowncodeblocks.js.map
\No newline at end of file