Skip to content

Commit 611ac16

Browse files
committed
add folding for markdown mode
1 parent e5fe3a4 commit 611ac16

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

‎lib/ace/mode/folding/markdown.js‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* ***** BEGIN LICENSE BLOCK *****
2+
* Distributed under the BSD license:
3+
*
4+
* Copyright (c) 2010, Ajax.org B.V.
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* * Neither the name of Ajax.org B.V. nor the
15+
* names of its contributors may be used to endorse or promote products
16+
* derived from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
22+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
* ***** END LICENSE BLOCK ***** */
30+
31+
define(function(require, exports, module) {
32+
"use strict";
33+
34+
var oop = require("../../lib/oop");
35+
var BaseFoldMode = require("./fold_mode").FoldMode;
36+
var Range = require("../../range").Range;
37+
38+
var FoldMode = exports.FoldMode = function() {};
39+
oop.inherits(FoldMode, BaseFoldMode);
40+
41+
(function() {
42+
this.foldingStartMarker = /^(?:[=-]+\s*$|#{1,6} |`{3})/;
43+
44+
this.getFoldWidget = function(session, foldStyle, row) {
45+
var line = session.getLine(row);
46+
if (!this.foldingStartMarker.test(line))
47+
return "";
48+
49+
if (line[0] == "`") {
50+
if (session.bgTokenizer.getState(row) == "start")
51+
return "end";
52+
return "start";
53+
}
54+
55+
return "start";
56+
};
57+
58+
this.getFoldWidgetRange = function(session, foldStyle, row) {
59+
var line = session.getLine(row);
60+
var startColumn = line.length;
61+
var maxRow = session.getLength();
62+
var startRow = row;
63+
var endRow = row;
64+
if (!line.match(this.foldingStartMarker))
65+
return;
66+
67+
if (line[0] == "`") {
68+
if (session.bgTokenizer.getState(row) !== "start") {
69+
while (++row < maxRow) {
70+
line = session.getLine(row);
71+
if (line[0] == "`" & line.substring(0, 3) == "```")
72+
break;
73+
}
74+
return new Range(startRow, startColumn, row, 0);
75+
} else {
76+
while (row -- > 0) {
77+
line = session.getLine(row);
78+
if (line[0] == "`" & line.substring(0, 3) == "```")
79+
break;
80+
}
81+
return new Range(row, line.length, startRow, 0);
82+
}
83+
}
84+
85+
var token;
86+
function isHeading(row) {
87+
token = session.getTokens(row)[0];
88+
return token && token.type.lastIndexOf(heading, 0) === 0;
89+
}
90+
91+
var heading = "markup.heading";
92+
function getLevel() {
93+
var ch = token.value[0];
94+
if (ch == "=") return 6;
95+
if (ch == "-") return 5;
96+
return 7 - token.value.search(/[^#]/);
97+
}
98+
99+
if (isHeading(row)) {
100+
var startHeadingLevel = getLevel();
101+
while (++row < maxRow) {
102+
if (!isHeading(row))
103+
continue;
104+
var level = getLevel();
105+
if (level >= startHeadingLevel)
106+
break;
107+
}
108+
109+
endRow = row - (!token || ["=", "-"].indexOf(token.value[0]) == -1 ? 1 : 2);
110+
111+
if (endRow > startRow) {
112+
while (endRow > startRow && /^\s*$/.test(session.getLine(endRow)))
113+
endRow--;
114+
}
115+
116+
if (endRow > startRow) {
117+
var endColumn = session.getLine(endRow).length;
118+
return new Range(startRow, startColumn, endRow, endColumn);
119+
}
120+
}
121+
};
122+
123+
}).call(FoldMode.prototype);
124+
125+
});

‎lib/ace/mode/markdown.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var XmlMode = require("./xml").Mode;
3838
var HtmlMode = require("./html").Mode;
3939
var Tokenizer = require("../tokenizer").Tokenizer;
4040
var MarkdownHighlightRules = require("./markdown_highlight_rules").MarkdownHighlightRules;
41+
var MarkdownFoldMode = require("./folding/markdown").FoldMode;
4142

4243
var Mode = function() {
4344
var highlighter = new MarkdownHighlightRules();
@@ -49,6 +50,8 @@ var Mode = function() {
4950
"xml-": XmlMode,
5051
"html-": HtmlMode
5152
});
53+
54+
this.foldingRules = new MarkdownFoldMode();
5255
};
5356
oop.inherits(Mode, TextMode);
5457

0 commit comments

Comments
 (0)