diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..545a18a --- /dev/null +++ b/.babelrc @@ -0,0 +1,10 @@ +{ + "presets": [ + [ + "@babel/preset-env", { + loose: true, + } + ] + ] +} + diff --git a/.browserslistrc b/.browserslistrc new file mode 100644 index 0000000..c86dca6 --- /dev/null +++ b/.browserslistrc @@ -0,0 +1,4 @@ +>0.25% +not ie 11 +not dead +not op_mini all diff --git a/.eslintrc b/.eslintrc index ef2fee8..c27e772 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,20 +1,25 @@ { - extends: [ '@borgar/eslint-config' ], - parserOptions: { - ecmaVersion: 2020, - sourceType: 'module', + "extends": [ + "@borgar/eslint-config", + "@borgar/eslint-config/jsdoc" + ], + "parserOptions": { + "ecmaVersion": 2020, + "sourceType": "module", }, - rules: { - 'import/export': 'error', - 'import/no-unresolved': 'error', - 'no-use-before-define': 'error' + "rules": { + "import/export": "error", + "import/no-unresolved": "error", + "no-use-before-define": "error", + "no-mixed-operators": "off", + "no-multiple-empty-lines": [ "error", { "max": 2, "maxBOF": 0, "maxEOF": 1 }] }, - globals: { - require, - module, - exports + "globals": { + "require", + "module", + "exports" }, - plugins: [ - 'import' + "plugins": [ + "import" ] } diff --git a/.npmignore b/.npmignore index d375829..5f9e979 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ +_* docs bin test diff --git a/API.md b/API.md new file mode 100644 index 0000000..8e00885 --- /dev/null +++ b/API.md @@ -0,0 +1,1358 @@ +# textile-js API + +**Classes** + +- [CommentNode( data )](#CommentNode) + - [.constructor( data )](#CommentNode.constructor) + - [.data](#CommentNode.data) + - [.nodeType](#CommentNode.nodeType) + - [.pos](#CommentNode.pos) + - [.setPos( start, length )](#CommentNode.setPos) + - [.toHTML()](#CommentNode.toHTML) + - [.visit( fn )](#CommentNode.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [Document()](#Document) + - [.appendChild( node )](#Document.appendChild) + - [.children](#Document.children) + - [.firstChild()](#Document.firstChild) + - [.nodeType](#Document.nodeType) + - [.pos](#Document.pos) + - [.setPos( start, length )](#Document.setPos) + - [.toHTML()](#Document.toHTML) + - [.visit( fn )](#Document.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [Element( tagName, attr )](#Element) + - [.constructor( tagName, attr )](#Element.constructor) + - [.appendChild( node )](#Element.appendChild) + - [.attr](#Element.attr) + - [.children](#Element.children) + - [.firstChild()](#Element.firstChild) + - [.getAttribute( name )](#Element.getAttribute) + - [.insertBefore( newNode, referenceNode )](#Element.insertBefore) + - [.nodeType](#Element.nodeType) + - [.pos](#Element.pos) + - [.reIndent( shiftBy )](#Element.reIndent) + - [.removeChild( oldNode )](#Element.removeChild) + - [.setAttr( attr )](#Element.setAttr) + - [.setAttribute( name, value )](#Element.setAttribute) + - [.setPos( start, length )](#Element.setPos) + - [.tagName](#Element.tagName) + - [.toHTML()](#Element.toHTML) + - [.visit( fn )](#Element.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [ExtendedNode()](#ExtendedNode) + - [.appendChild( node )](#ExtendedNode.appendChild) + - [.children](#ExtendedNode.children) + - [.nodeType](#ExtendedNode.nodeType) + - [.pos](#ExtendedNode.pos) + - [.setPos( start, length )](#ExtendedNode.setPos) + - [.toHTML()](#ExtendedNode.toHTML) + - [.visit( fn )](#ExtendedNode.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [HiddenNode( data )](#HiddenNode) + - [.constructor( data )](#HiddenNode.constructor) + - [.data](#HiddenNode.data) + - [.nodeType](#HiddenNode.nodeType) + - [.pos](#HiddenNode.pos) + - [.setPos( start, length )](#HiddenNode.setPos) + - [.toHTML()](#HiddenNode.toHTML) + - [.visit( fn )](#HiddenNode.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [Node()](#Node) + - [.nodeType](#Node.nodeType) + - [.pos](#Node.pos) + - [.setPos( start, length )](#Node.setPos) + - [.toHTML()](#Node.toHTML) + - [.visit( fn )](#Node.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [RawNode( data )](#RawNode) + - [.constructor( data )](#RawNode.constructor) + - [.data](#RawNode.data) + - [.nodeType](#RawNode.nodeType) + - [.pos](#RawNode.pos) + - [.setPos( start, length )](#RawNode.setPos) + - [.toHTML()](#RawNode.toHTML) + - [.visit( fn )](#RawNode.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) +- [TextNode( data )](#TextNode) + - [.constructor( data )](#TextNode.constructor) + - [.data](#TextNode.data) + - [.nodeType](#TextNode.nodeType) + - [.pos](#TextNode.pos) + - [.setPos( start, length )](#TextNode.setPos) + - [.toHTML()](#TextNode.toHTML) + - [.visit( fn )](#TextNode.visit) + - [`static`COMMENT_NODE](#COMMENT_NODE) + - [`static`DOCUMENT_NODE](#DOCUMENT_NODE) + - [`static`ELEMENT_NODE](#ELEMENT_NODE) + - [`static`EXTENDED_NODE](#EXTENDED_NODE) + - [`static`HIDDEN_NODE](#HIDDEN_NODE) + - [`static`NODE](#NODE) + - [`static`RAW_NODE](#RAW_NODE) + - [`static`TEXT_NODE](#TEXT_NODE) + +**Functions** + +- [parseTree( source, options )](#parseTree) +- [textile( source, options )](#textile) + +**Type** + +- [PosData](#PosData) + +## Classes + +### # CommentNode( data ) extends [`Node`](#Node) + +Textile VDOM comment node. + +--- + +#### # .constructor( data ) + +Constructs a new CommentNode + +##### Parameters + +| Name | Type | Description | +| ---- | -------- | ---------------------- | +| data | `string` | The node's string data | + +--- + +#### # .data + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # Document() extends [`Node`](#Node) + +Textile VDOM document node. + +--- + +#### # .appendChild( node ) ⇒ [`Node`](#Node) + +Appends a node as a direct child of the current element. + +##### Parameters + +| Name | Type | Description | +| ---- | --------------- | --------------- | +| node | [`Node`](#Node) | The node to add | + +##### Returns + +[`Node`](#Node) – The argument node is returned unchanged. + +--- + +#### # .children + +--- + +#### # .firstChild() ⇒ `void` + +The first child of this element. + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # Element( tagName, attr ) extends [`Node`](#Node) + +Textile VDOM Element node. + +--- + +#### # .constructor( tagName, attr ) + +Constructs a new Element. + +##### Parameters + +| Name | Type | Description | +| ------- | ------------------------ | -------------------------- | +| tagName | `string` | A tag name for the element | +| attr | `Record` | A dictionary of attributes | + +--- + +#### # .appendChild( node ) ⇒ [`Node`](#Node) + +Appends a node as a direct child of the current element. + +##### Parameters + +| Name | Type | Description | +| ---- | --------------- | --------------- | +| node | [`Node`](#Node) | The node to add | + +##### Returns + +[`Node`](#Node) – The argument node is returned unchanged. + +--- + +#### # .attr + +--- + +#### # .children + +--- + +#### # .firstChild() ⇒ `void` + +The first child of this element. + +--- + +#### # .getAttribute( name ) ⇒ `string` | `null` + +Read an attribute of this element. + +##### Parameters + +| Name | Type | Description | +| ---- | -------- | ------------------------- | +| name | `string` | The name of the attribute | + +##### Returns + +`string` | `null` – The attribute value + +--- + +#### # .insertBefore( newNode, referenceNode ) ⇒ [`Node`](#Node) + +Insert a node immediatly before another node + +##### Parameters + +| Name | Type | Description | +| ------------- | --------------- | ------------------------------- | +| newNode | [`Node`](#Node) | The new node to insert | +| referenceNode | [`Node`](#Node) | The node which to insert before | + +##### Returns + +[`Node`](#Node) – The newly inserted node + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .reIndent( shiftBy ) ⇒ [`Element`](#Element) + +Add or drop tab indentation levels within the element. + +##### Parameters + +| Name | Type | Description | +| ------- | -------- | ---------------------------------------------- | +| shiftBy | `number` | How much to increase/decrease the intentation. | + +##### Returns + +[`Element`](#Element) – The current element. + +--- + +#### # .removeChild( oldNode ) ⇒ [`Node`](#Node) + +Removes a child from the current element. + +##### Parameters + +| Name | Type | Description | +| ------- | --------------- | ------------------------------------------------- | +| oldNode | [`Node`](#Node) | The node that should be detachde from this parent | + +##### Returns + +[`Node`](#Node) – The detached node + +--- + +#### # .setAttr( attr ) ⇒ `void` + +Apply a set attributes onto this element. + +##### Parameters + +| Name | Type | Description | +| ---- | ------------------------ | ----------------------------- | +| attr | `Record` | A dict of attributes to apply | + +--- + +#### # .setAttribute( name, value ) ⇒ `void` + +Set the attribute of this element. + +##### Parameters + +| Name | Type | Description | +| ----- | ------------------ | ------------------------- | +| name | `string` | The name of the attribute | +| value | `string` \| `null` | The attribute value | + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .tagName + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # ExtendedNode() extends [`Node`](#Node) + +Textile VDOM extended node container. + +A container for the nodes that are a part of the same extended block. + +--- + +#### # .appendChild( node ) ⇒ [`Node`](#Node) + +Appends a node as a direct child of the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | --------------- | --------------- | +| node | [`Node`](#Node) | The node to add | + +##### Returns + +[`Node`](#Node) – The argument node is returned unchanged. + +--- + +#### # .children + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # HiddenNode( data ) extends [`Node`](#Node) + +Textile VDOM hidden node. + +This node type is used to capture things that appear in the textile markup, but do not need to be processed or rendered. + +--- + +#### # .constructor( data ) + +Constructs a new HiddenNode + +##### Parameters + +| Name | Type | Description | +| ---- | -------- | ---------------------- | +| data | `string` | The node's string data | + +--- + +#### # .data + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # Node() + +A basic textile VDOC node. + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # RawNode( data ) extends [`Node`](#Node) + +Textile VDOM raw-text node. + +Essentially this is the same as a TextNode except it does not merge with textnodes, and is not post-processed by glyph replacers etc. + +--- + +#### # .constructor( data ) + +Constructs a new RawNode + +##### Parameters + +| Name | Type | Description | +| ---- | -------- | ---------------------- | +| data | `string` | The node's string data | + +--- + +#### # .data + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +### # TextNode( data ) extends [`Node`](#Node) + +Textile VDOM text node. + +--- + +#### # .constructor( data ) + +Constructs a new TextNode + +##### Parameters + +| Name | Type | Description | +| ---- | -------- | ---------------------- | +| data | `string` | The node's string data | + +--- + +#### # .data + +--- + +#### # .nodeType + +TypeID of node + +--- + +#### # .pos + +Position data for the node + +--- + +#### # .setPos( start, length ) ⇒ [`Node`](#Node) + +Sets the source position of the node. + +##### Parameters + +| Name | Type | Description | +| ------ | -------- | ------------------------ | +| start | `number` | The start position | +| length | `number` | The length of the source | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # .toHTML() ⇒ `string` + +Emit the HTML source representation of this node and its children. + +##### Returns + +`string` – HTML source string. + +--- + +#### # .visit( fn ) ⇒ [`Node`](#Node) + +Visit this function and all its descendants. + +The visitor callback will be called for the node and every child in its subtree. It will be supplied a single argument which will be the current node. + +##### Parameters + +| Name | Type | Description | +| ---- | ---------- | ----------------------------- | +| fn | `Function` | The visitor callback function | + +##### Returns + +[`Node`](#Node) – The context node + +--- + +#### # `static`COMMENT_NODE + +Set to 8 + +--- + +#### # `static`DOCUMENT_NODE + +Set to 9 + +--- + +#### # `static`ELEMENT_NODE + +Set to 1 + +--- + +#### # `static`EXTENDED_NODE + +Set to -3 + +--- + +#### # `static`HIDDEN_NODE + +Set to -2 + +--- + +#### # `static`NODE + +Set to 0 + +--- + +#### # `static`RAW_NODE + +Set to -1 + +--- + +#### # `static`TEXT_NODE + +Set to 3 + +--- + +## Functions + +### # parseTree( source, options ) ⇒ [`Document`](#Document) + +Parse Textile markup and return a "VDOM" tree. + +##### Parameters + +| Name | Type | Default | Description | +| ---------------------------- | --------------------- | ------- | ------------------------------------------------------------------------------------------- | +| source | `string` | | The source transmit | +| options | `object` | | Parsing options | +| options.[allowed_block_tags] | `Array` | | Which HTML tags in the document trigger HTML parsing (def: div, blockquote, ...) | +| options.[auto_backlink] | `boolean` | `true` | Automatically backlink footnotes, regardless of syntax used | +| options.[blocked_uri] | `Array` | | A list of blocked href protocols (def: javascript, vbscript, data) | +| options.[breaks] | `boolean` | `true` | Convert single-line linebreaks to
| +| options.[glyph_entities] | `boolean` | `true` | Convert entity markup (->) to glyphs (→) | +| options.[id_prefix] | `boolean` \| `string` | `true` | Footnotes and endnote HTML IDs are prefixed with a string (as set here) or number (if true) | + +##### Returns + +[`Document`](#Document) – A textile Document node + +--- + +### # textile( source, options ) ⇒ `string` + +Convert Textile markup to HTML markup. + +##### Parameters + +| Name | Type | Default | Description | +| ---------------------------- | --------------------- | ------- | ------------------------------------------------------------------------------------------- | +| source | `string` | | The source transmit | +| options | `object` | | Parsing options | +| options.[allowed_block_tags] | `Array` | | Which HTML tags in the document trigger HTML parsing (def: div, blockquote, ...) | +| options.[auto_backlink] | `boolean` | `true` | Automatically backlink footnotes, regardless of syntax used | +| options.[blocked_uri] | `Array` | | A list of blocked href protocols (def: javascript, vbscript, data) | +| options.[breaks] | `boolean` | `true` | Convert single-line linebreaks to
| +| options.[glyph_entities] | `boolean` | `true` | Convert entity markup (->) to glyphs (→) | +| options.[id_prefix] | `boolean` \| `string` | `true` | Footnotes and endnote HTML IDs are prefixed with a string (as set here) or number (if true) | + +##### Returns + +`string` – HTML source string + +--- + +## Type + +### # PosData = `object` + +Offsets in the Textile source for this node + +##### Properties + +| Name | Type | Description | +| ------- | -------- | --------------------- | +| [end] | `number` | Where the node ends | +| [start] | `number` | Where the node starts | + +--- + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..2314fcf --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +# textile.js + +_Textile.js_ is a fully featured implementation of Textile parser/converter in JavaScript that runs reasonably fast and avoids outputting broken HTML. + +Give it a go in [a live textile web editor](http://borgar.github.com/textile-js/). + + +## Install + +The library is available + +```sh +npm install textile-js +``` + + +## Usage + +```js +import { textile } from 'textile-js'; + +console.log(textile("I am using __textile__.")); +``` + +You may supply a number of options to the converter: + +```js +textile("I am using __textile__.", { breaks: false }) +``` + +* `breaks` - Used to disable the default behavior of line-breaking single newlines within blocks. +* `auto_backlink` - Turn this on to have footnotes automatically link back to their references (otherwise enabled by syntax: `fn1^`). +* `glyph_entities` - Allows disabling of processing glyph syntax ([->]) to glyphs (→) +* [...and more](./API.md#textile) + +You can also get to the parsed document tree: + +```js +const vdom = textile.parse(text); +console.log(vdom); +``` + +See [API documentation](./API.md) for more detailed descriptions. + + +## CLI interface included + +``` +$ textile -o hello.html +hello world +^D +$ cat hello.html +

hello world

+``` + +Usage: `textile [-o outputfile] [inputfile]` + +If no input file is provided, the program will listen to data on `stdin`. + + +## License + +Copyright © 2012, Borgar Þorsteinsson (MIT License). + +See [LICENSE](./LICENSE) diff --git a/README.textile b/README.textile deleted file mode 100644 index 7f85852..0000000 --- a/README.textile +++ /dev/null @@ -1,43 +0,0 @@ -h1. textile.js - -Attempt at an implementation of fully featured Textile parser in JavaScript that runs reasonably fast and mostly avoids outputting broken HTML. - -Give it a go in "a live textile web editor":http://borgar.github.com/textile-js/. - - -h2. Install - -bc. $ npm install textile-js - - -h2. Options - -The basic interface mimics "marked":https://github.com/chjj/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 supported option is @breaks@ which can be used to enable/disable the default behavior of line-breaking single newlines within blocks. - - -h2. Usage - -bc. console.log( textile( "I am using __textile__." ) ); - -You can also get to the syntax tree, which uses "JsonML":http://www.jsonml.org/. - -bc. var jsonml = textile.parse( text ); -console.log( jsonml ); - - -h2. CLI - -bc. $ textile -o hello.html -hello world -^D -$ cat hello.html -

hello world

- - -h2. License - -Copyright (c) 2012, Borgar Þorsteinsson (MIT License). - -See LICENSE. diff --git a/bin/textile b/bin/textile deleted file mode 100755 index eccd0e3..0000000 --- a/bin/textile +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env node - -var fs = require( 'fs' ); - -var textile; -var nodeVersion = process && process.versions && process.versions.node; -// is this node version mostly ES6 complete -if ( parseInt( nodeVersion || 0, 10 ) > 5 ) { - // serve code from src dir - textile = require( '../src' ); -} -else { - // serve transpiled code - textile = require( '../dist/textile' ); -} - -// clean arguments -var args = []; -process.argv.slice( 2 ).forEach( function ( m, i, s ) { - if ( ( s = /^([^=]+)=(.*)$/.exec( m ) ) ) { - args.push( s[1], s[2] ); - } - else { - args.push( m ); - } -}); - -// parse arguments -var options = {}; -while ( args.length ) { - var arg = args.shift(); - if ( arg === '-t' || arg === '--tokens' ) { - options.tokens = true; - } - else if ( arg === '-i' || arg === '--input' ) { - options.input = args.shift(); - } - else if ( arg === '-o' || arg === '--output' ) { - options.output = args.shift(); - } - else { - if ( !options.files ) { options.files = []; } - options.files.shift( arg ); - } -} - -// input overrides file arguments -if ( options.input ) { - options.files = [ options.input ]; -} - -// writer -var data = ''; -function writeData () { - data = options.tokens ? JSON.stringify( textile.jsonml( data ), null, 2 ) - : textile( data ); - if ( options.output ) { - fs.writeFileSync( options.output, data ); - } - else { - process.stdout.write( data + '\n' ); - } -} - -// file or stdin? -if ( options.files ) { - data = fs.readFileSync( options.files[0], 'utf8' ); - writeData(); -} -else { - const stdin = process.stdin; - stdin.setEncoding( 'utf8' ); - stdin.on( 'data', function ( text ) { - data += text; - }); - stdin.on( 'end', writeData ); - stdin.resume(); -} diff --git a/bin/textile.js b/bin/textile.js new file mode 100755 index 0000000..3acef8a --- /dev/null +++ b/bin/textile.js @@ -0,0 +1,58 @@ +#!/usr/bin/env node +/* globals process */ +import fs from 'fs'; +import textile from '../src/index.js'; + +// clean arguments +const args = []; +process.argv.slice(2).forEach(function (m, i, s) { + if ((s = /^([^=]+)=(.*)$/.exec(m))) { + args.push(s[1], s[2]); + } + else { + args.push(m); + } +}); + +// parse arguments +const options = {}; +while (args.length) { + const arg = args.shift(); + if (arg === '-o' || arg === '--output') { + options.output = args.shift(); + } + else { + if (!options.files) { options.files = []; } + options.files.push(arg); + } +} + +// input overrides file arguments +if (options.input) { + options.files = [ options.input ]; +} + +// writer +let data = ''; +function writeData () { + const output = textile(data); + if (options.output) { + fs.writeFileSync(options.output, output); + } + else { + process.stdout.write(output + '\n'); + } +} + +// file or stdin? +if (options.files) { + data = fs.readFileSync(options.files[0], 'utf8'); + writeData(); +} +else { + const stdin = process.stdin; + stdin.setEncoding('utf8'); + stdin.on('data', text => (data += text)); + stdin.on('end', writeData); + stdin.resume(); +} diff --git a/docs/app.css b/docs/app.css index 8f8af8d..90492ce 100644 --- a/docs/app.css +++ b/docs/app.css @@ -11,15 +11,14 @@ body { padding: 0; height: 100%; - display: flex; - flex-direction: column; - align-content: stretch; - -} - -body > * { - flex: 0 1 auto; - align-self: auto; + display: grid; + grid-template-columns: 1fr 1fr; + grid-template-rows: 55px 25px 1fr; + gap: 0px 6px; + grid-template-areas: + "header header" + "txHead oHead" + "txBody oBody"; } body > h1 { @@ -27,86 +26,59 @@ body > h1 { margin: 0; font-size: 20px; color: #444; + grid-area: header; } -textarea { - font-family: "andale mono", "lucida console", monospace; - font-size: 12px; - display: block; - resize: none; -} - -.row { - margin: 0 0; - - flex: 1 1 auto; - align-self: stretch; - - display: flex; - flex-direction: row; - flex-wrap: nowrap; - align-items: stretch; -} - -.col { - position: relative; - box-sizing: border-box; - margin: 0 1rem 0 0; - padding: 0; - width: 50%; - - flex: 1 1 auto; - align-self: stretch; -} -.col:first-child { - margin: 0 1rem 0 1rem; +#tx_hd { + grid-area: txHead; } -.col { - display: flex; - flex-direction: column; - align-content: stretch; +#tx_body { + grid-area: txBody; + margin-top: -1px; + border-top: 1px solid #aaa; } -.col .hd { - flex: 0 1 auto; - align-self: auto; +#o_hd { + grid-area: oHead; } -.col > div, .col textarea { - flex: 1 1 auto; - align-self: stretch; +#o_body { + grid-area: oBody; + overflow: auto; + margin-top: -1px; + border-top: 1px solid #aaa; } - -label { - display: block; - padding: 4px 0; - font-weight: bold; - color: #777; -} -.tools { - position: absolute; - top: 0; - right: 0; - padding: 0; +#tx_body > * { + height: 100%; + box-sizing: border-box; + background: white; + font-family: consolas, "andale mono", "lucida console", monospace; + resize: none; + padding: 1rem; + width: 100%; + border: 0; + line-height: 1.25; + font-size: 14px; + outline: none; } - -.panel { - background: #fff; - border: 1px solid #aaaaaa; - outline: none; - display: block; - margin: 0; - padding: 4px; +#o_body > * { box-sizing: border-box; -} -#text_preview { - overflow: auto; + background: white; + padding: 1rem; + width: 100%; + border: 0; + color: #444; + min-height: 100%; } -#md_input { - background: #f6f6f6; +#syntax_guide, +#html_output { + white-space: pre-wrap; + font-size: 14px; + font-family: consolas, "andale mono", "lucida console", monospace; + line-height: 1.25; } #syntax_guide, @@ -114,22 +86,23 @@ label { display: none; } - -#credit1 { - background: url(http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png) -27px -15px no-repeat; +.td_hd { + position: relative; +} +.tools { position: absolute; - top: 0; - right: 0; - text-indent: -999em; - display: block; - overflow: hidden; - height: 108px; - width: 108px; - zoom: .8; + bottom: 2px; + right: 16px; + padding: 0; } - -#foot { - min-height: 1rem; +.tools button { + border: 0; + background: white; + color: #444; + padding: 2px 10px; + border-radius: 12px; + border-bottom: 1px solid #aaa; + cursor: pointer; } @@ -137,34 +110,47 @@ label { padding: 0; margin: 0; list-style: none; + height: 100%; + box-sizing: border-box; + padding-left: 10px; } .tab li { display: inline-block; + height: 100%; + box-sizing: border-box; } +.tab li label, .tab li a { + height: 100%; display: block; + box-sizing: border-box; padding: 4px 10px; text-decoration: none; color: #999; background: #eee; margin: 0 1px; } +.tab .current label, .tab .current a { color: #555; background: #fff; padding-bottom: 5px; - margin: -1px 0; + margin: 0; border: 1px solid #aaa; + border-bottom: 1px solid #fff; + z-index: 213; + position: relative; } #text_preview { - padding: 0 1em 4px 1em; - color: #444; - font-size: 13px; + font-size: 16px; line-height: 1.4; } +#text_preview > *:first-child { + margin-top: 0; +} #text_preview h1 { line-height: 1.1; font-size: 2em; @@ -195,10 +181,15 @@ label { #text_preview table { border-collapse : collapse; } +#text_preview th { + text-align: left; +} #text_preview table th, #text_preview table td { padding : 4px 8px; border : 1px solid #aaa; + text-align: left; + vertical-align : top; } #text_preview ul { margin-left: 0; @@ -222,3 +213,4 @@ label { text-transform: lowercase; letter-spacing: .1em; } + diff --git a/docs/app.js b/docs/app.js index a7d084c..138bb95 100644 --- a/docs/app.js +++ b/docs/app.js @@ -1,6 +1,5 @@ /* globals document fetch textile localStorage setTimeout clearTimeout */ (function () { - function getElm (selector) { return document.querySelector(selector); } @@ -47,7 +46,7 @@ processing_time = endTime - startTime; text_preview.innerHTML = html; - html_output.value = html; + html_output.textContent = html; // save last output text to storage if we have it if (!input.value || input.value === help_text) { @@ -59,7 +58,6 @@ } - // $('.tab').minitabs(); const tabs = Array.from(document.querySelectorAll('.tab a')); tabs.forEach(d => { d.addEventListener('click', e => { @@ -76,7 +74,7 @@ let convertTextTimer; - attach('#tx_input', 'keyup', e => { + attach('#tx_input', 'keyup', () => { clearTimeout(convertTextTimer); const defer_time = Math.min(processing_time, max_delay); convertTextTimer = setTimeout(convert_text, defer_time); @@ -92,7 +90,7 @@ // load syntax guide loadText('syntax.txt', txt => { - getElm('#syntax_guide').value = txt; + getElm('#syntax_guide').textContent = txt; }); // load help text @@ -103,10 +101,9 @@ } }); - attach('button.clear', 'click', e => { + attach('button.clear', 'click', () => { convert_text(''); }); convert_text(); - })(); diff --git a/docs/index.html b/docs/index.html index d2ffc59..135c965 100644 --- a/docs/index.html +++ b/docs/index.html @@ -10,31 +10,30 @@

Textile live web editor

-
-
-
- - - - -
- -
+
-
- -
- - -
+ + + +
-