1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-29 08:43:14 +00:00
TiddlyWiki5/core/modules/parsers/wikiparser/rules/emphasis.js
2012-12-22 23:09:44 +00:00

80 lines
1.4 KiB
JavaScript

/*\
title: $:/core/modules/parsers/wikiparser/rules/emphasis.js
type: application/javascript
module-type: wikirule
Wiki text inline rule for emphasis. For example:
```
This is ''bold'' text
This is //italic// text
This is __underlined__ text
This is ^^superscript^^ text
This is ,,subscript,, text
This is ~~strikethrough~~ text
```
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "emphasis";
exports.types = {inline: true};
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /''|\/\/|__|\^\^|,,|~~/mg;
};
exports.parse = function() {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Figure out which element and closing regexp to use
var tag,reEnd;
switch(this.match[0]) {
case "''": // Bold
tag = "strong";
reEnd = /''/mg;
break;
case "//": // Italics
tag = "em";
reEnd = /\/\//mg;
break;
case "__": // Underline
tag = "u";
reEnd = /__/mg;
break;
case "^^": // Superscript
tag = "sup";
reEnd = /\^\^/mg;
break;
case ",,": // Subscript
tag = "sub";
reEnd = /,,/mg;
break;
case "~~": // Strikethrough
tag = "strike";
reEnd = /~~/mg;
break;
}
// Parse the run including the terminator
var tree = this.parser.parseInlineRun(reEnd,{eatTerminator: true});
// Return the classed span
return [{
type: "element",
tag: tag,
children: tree
}];
};
})();