-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfind-diagram.js
49 lines (40 loc) · 1.41 KB
/
find-diagram.js
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
43
44
45
46
47
48
49
'use strict';
module.exports = findDiagram;
const vscode = require('vscode');
// http://stackoverflow.com/a/6969486/1977815
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
const findDiagramWithRegExp = (re, text, cursor) => {
let regexp = new RegExp(re.source, re.flags);
let index = regexp.last;
let diagram;
let array;
while (!diagram && (array = regexp.exec(text)) !== null) {
const start = text.indexOf(array[1], index);
const end = start + array[1].length;
if (start > 0 && start <= cursor && cursor <= end) {
diagram = array[1];
} else {
index = regexp.lastIndex;
}
}
return diagram;
};
function findDiagram(text, cursor) {
const re = {
html: /<div class="mermaid">([\s\S]*?)<\/div>/g,
hugo: /\{\{<mermaid.*>\}\}([\s\S]*?)\{\{<\/mermaid>\}\}/g,
markdown: /```mermaid[= ,;\(\)\?"\w]*$([\s\S]*?)```/gm,
// the whitespace between ::: and mermaid is for Azure DevOps Wiki
vsts: /::: ?mermaid([\s\S]*?):::/g,
sphinx: /\.\. mermaid::(?:[ \t]*)?$(?:(?:\n[ \t]+:(?:(?:\\:\s)|[^:])+:[^\n]*$)+\n)?((?:\n(?:[ \t][^\n]*)?$)+)?/gm,
};
return (
findDiagramWithRegExp(re.html, text, cursor) ||
findDiagramWithRegExp(re.markdown, text, cursor) ||
findDiagramWithRegExp(re.vsts, text, cursor) ||
findDiagramWithRegExp(re.hugo, text, cursor) ||
findDiagramWithRegExp(re.sphinx, text, cursor)
);
}