Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
51007 views
1
CodeMirror.registerHelper("fold", "markdown", function(cm, start) {
2
var maxDepth = 100;
3
4
function isHeader(lineNo) {
5
var tokentype = cm.getTokenTypeAt(CodeMirror.Pos(lineNo, 0));
6
return tokentype && /\bheader\b/.test(tokentype);
7
}
8
9
function headerLevel(lineNo, line, nextLine) {
10
var match = line && line.match(/^#+/);
11
if (match && isHeader(lineNo)) return match[0].length;
12
match = nextLine && nextLine.match(/^[=\-]+\s*$/);
13
if (match && isHeader(lineNo + 1)) return nextLine[0] == "=" ? 1 : 2;
14
return maxDepth;
15
}
16
17
var firstLine = cm.getLine(start.line), nextLine = cm.getLine(start.line + 1);
18
var level = headerLevel(start.line, firstLine, nextLine);
19
if (level === maxDepth) return undefined;
20
21
var lastLineNo = cm.lastLine();
22
var end = start.line, nextNextLine = cm.getLine(end + 2);
23
while (end < lastLineNo) {
24
if (headerLevel(end + 1, nextLine, nextNextLine) <= level) break;
25
++end;
26
nextLine = nextNextLine;
27
nextNextLine = cm.getLine(end + 2);
28
}
29
30
return {
31
from: CodeMirror.Pos(start.line, firstLine.length),
32
to: CodeMirror.Pos(end, cm.getLine(end).length)
33
};
34
});
35
36