Attempt at an implementation of fully featured Textile parser in JavaScript that runs reasonably fast and mostly avoids outputting broken HTML.
Please Note : This is a fork containing some changes and fixes, used for my Visual Studio Code extension Textile Language Features :
- map each HTML output tag, to the corresponding source code line;
- fix for bug borgar/textile-js#52;
- add configurable hooks, to modify JsonML nodes before and when rendering to HTML.
Give it a go in a live textile web editor.
$ npm install textile-js
The basic interface mimics marked, the popular markdown parser. So if you use that in your project then you can support Textile as well with minimal effort.
Currently, the only option imported from marked is breaks
which can be used to enable/disable the default behavior of line-breaking single newlines within blocks.
textile( textileString [, options] )
Argument | Type | Notes |
textileString | string |
String of textile source to be compiled. |
options | object |
Hash of options. Can also use textile.setOptions . |
Member | Type | Default | Notes |
break | boolean |
true |
Convert single-line linebreaks to <br> . |
cssClassOriginalLineNumber | string |
'' |
Add this CSS class name, to each elements of HTML output, mapped to a the line numbers of input text. Works only when showOriginalLineNumber option is enabled. |
hooks |
|
[] |
Array of function (and optional initial parameter), applied in the order of the array to each JsonML node before rendering to HTML. The function receives a JsonML node (JsonML sub-tree), and returns it with modifications. |
lineOffset | integer |
0 |
Line number offset of the first char in input text, for showOriginalLineNumber option. |
renderers |
|
[] |
Rendering functions, called with the tagName, the attributes object, the content string, and a context variable :content is returned to replace the content string of a tag. |
showOriginalLineNumber | boolean |
false |
Maps the elements of HTML output, with the line numbers of input text. An HTML attribute data-line is added to the HTML block elements = the line number in input text, of the first char of text inside that HTML block.For tags inlined inside textile source, a HTML attribute data-line-end is added too = the line number in input text, of the last char of the closing tag of that HTML block. |
dontEscapeContentForTags | [string] |
[] |
Don’t escape the content of every tag of this list, and of all their children in the generated HTML tree. |
console.log( textile( "I am using __textile__." ) );
You can also get to the syntax tree, which uses JsonML.
var jsonml = textile.parse( text );
console.log( jsonml );
$ textile -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>
Copyright © 2012, Borgar Þorsteinsson (MIT License).
See LICENSE.