mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-07-17 09:24:30 +00:00
![Jeremy Ruston](/assets/img/avatar_default.png)
This is a half-way through a big refactoring of the parsing and rendering infrastructure. The main change is to separate the parse and render trees, which makes the code a lot cleaner. The new parser isn't yet functional enough to replace the existing parser so for the moment you have to manually invoke it with `$tw.testNewParser()` in your browser console. I really ought to use branches for this kind of thing...
58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
/*\
|
|
title: $:/core/modules/parsers/wikiparser/rules/block/heading.js
|
|
type: application/javascript
|
|
module-type: wikiblockrule
|
|
|
|
Wiki text block rule for headings
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
var HeadingRule = function(parser,startPos) {
|
|
// Save state
|
|
this.parser = parser;
|
|
// Regexp to match
|
|
this.reMatch = /(!{1,6})/mg;
|
|
// Get the first match
|
|
this.matchIndex = startPos-1;
|
|
this.findNextMatch(startPos);
|
|
};
|
|
|
|
HeadingRule.prototype.findNextMatch = function(startPos) {
|
|
if(this.matchIndex !== undefined && startPos > this.matchIndex) {
|
|
this.reMatch.lastIndex = startPos;
|
|
this.match = this.reMatch.exec(this.parser.source);
|
|
this.matchIndex = this.match ? this.match.index : undefined;
|
|
}
|
|
return this.matchIndex;
|
|
};
|
|
|
|
/*
|
|
Parse the most recent match
|
|
*/
|
|
HeadingRule.prototype.parse = function() {
|
|
// Get all the details of the match
|
|
var headingLevel = this.match[1].length;
|
|
// Move past the !s
|
|
this.parser.pos = this.reMatch.lastIndex;
|
|
// Parse the heading
|
|
var classedRun = this.parser.parseClassedRun(/(\r?\n)/mg);
|
|
// Return the heading
|
|
return [{
|
|
type: "element",
|
|
tag: "h" + this.match[1].length,
|
|
attributes: {
|
|
"class": {type: "string", value: classedRun["class"]}
|
|
},
|
|
children: classedRun.tree
|
|
}];
|
|
};
|
|
|
|
exports.HeadingRule = HeadingRule;
|
|
|
|
})();
|