Constructor
new KozMUL(operationsopt, optionsopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
operations |
Object |
<optional> |
Determines which transformations will be applied to text. See the example below. |
options |
Object |
<optional> |
Currently, there is only one option: targetBlank. If you set this field to true, all links will be transformed with 'target="_blank"' attribute and, thus, opened in new tabs. |
Throws:
Either KozExceptions.unresolvedDependency (if it exists in the scope) or a string exception.
Example
// By default, the following object is used as an "operations" object:
{
transformHTMLEntities: true,
transformBasicStyleTags: true,
transformTypographicalCharacters: true,
transformSubscriptAndSuperscriptTags: true,
transformParagraphs: true,
transformLists: true,
transformQuotations: true,
transformHorizontalLines: false,
transformHeadings: false,
transformLinks: false,
transformImages: false,
transformTables: false,
transformCodeTags: false,
transformSubstitutions: false
}
// And the following one as "options":
{
targetBlank: false,
tableClasses: ['table', 'bordered-table'],
listsWithIndentations: true,
mostImportantHeading: 3
}
// You are allowed to pass your own object consisting of fields you want to change. For instance, lets create an object performing all transformations and using targetBlank option:
let processor = new KozMUL({
transformHorizontalLines: true,
transformHeadings: true,
transformLinks: true,
transformImages: true,
transformTables: true,
transformCodeTags: true,
transformSubstitutions: true
}, {
targetBlank: true
});
Methods
_basicTransformations(text) → {string}
Removes redundant paragraphs around blockquotes and redundant line break at the end of the text if it exists. Look at the example below to see other transformations which are performed by this method.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
some collocation => some collocation
[[eol]] => <br/>
_removeEscapingCharacters(text) → {string}
As the name says, this function removes escaping characters (\).
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
_toSingleLine(text) → {string}
Replaces all line breaks with [[eol]]s.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
_transformBasicStyleTags(text) → {string}
Look at the example below to see what exactly this method replaces.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
**bold text** => <strong>bold text</strong>
__italic text__ => <em>italic text</em>
~~strikethrough text~~ => <s>strikethrough text</s>
_transformCodeTags(text) → {string}
Look at the example below to see what exactly this method replaces.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
``alert('Hello!')`` => <code>alert('Hello!')</code>
```function() { <pre><code>function() {
console.log('OK') => console.log('OK')</code></pre>
}```
_transformHeadings(text) → {string}
Look at the example below to see what exactly this method replaces. By default the most important heading is
. Use the mostImportantHeading option while creating an instance of the class to change this.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
== Heading 1[[eol]] => <h3>Heading 1</h3>
$$ Heading 2[[eol]] => <h4>Heading 2</h4>
%% Heading 3[[eol]] => <h5>Heading 3</h5>
_transformHorizontalLines(text) → {string}
Replaces 6 or more hypens (-) or 3 or more dashes (—) in the string without other characters to a line (
).
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
// Transforms the following string:
------
// into:
<hr/>
_transformHTMLEntities(text) → {string}
Gets rid of all HTML tags and does some replaces (see the example below).
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
& => &
< и > => < и >
" => "
' => '
_transformImages(text) → {string}
Look at the example below to see what exactly this method does.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
![[http://kozalo.ru/images/service/logo.png]] => <img src="http://kozalo.ru/images/service/logo.png">
![Logo][http://kozalo.ru/images/service/logo.png] => <img src="http://kozalo.ru/images/service/logo.png" alt="Logo">
_transformLinks(text) → {string}
Look at the example below to see what exactly this method replaces.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
// Due to technical restrictions, "@" in the example below has been changed to <at sign>.
<at sign>[[http://kozalo.ru]] => <a href="http://kozalo.ru">http://kozalo.ru</a>
<at sign>[My website][http://kozalo.ru] => <a href="http://kozalo.ru">My website</a>
_transformLists(text) → {string}
This function has 2 modes. Both are demonstrated in the example below.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
The first mode:
# One; + One;
# Two; + Two;
# Three. + Three.
# Nested one; # Nested one;
# Nested two; # Nested two;
# Nested three. + Four.
# Four. +
#
The second one:
## One; ++ One;
# Two; + Two;
# Three. + Three.
## Nested one; ## Nested one;
# Nested two; # Nested two;
### Nested three. #
### Four. +++ Four.
Both will be transformed into the following HTML code:
<ol> <ul>
<li>One;</li> <li>One;</li>
<li>Two;</li> <li>Two;</li>
<li>Three.</li> <li>Three.</li>
<ol> <ol>
<li>Nested one;</li> <li>Nested one;</li>
<li>Nested two;</li> <li>Nested two;</li>
<li>Nested three.</li> </ol>
</ol> <li>Four.</li>
<li>Four.</li> </ul>
</ol>
Note that the type of a list is determined on the first line. So, you can write something like this:
## Ordered list;
+ Still ordered.
_transformParagraphs(text) → {string}
Wraps text blocks with
-tags. The method tries to be as smart as possible: it extracts headings, lists and blockquotes from paragraphs to adhere the HTML specification.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
_transformQuotations(text) → {string}
Look at the example below to see what exactly this method does.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
// Transforms the text:
> Hello!
>
> My name is...
// into:
<blockquote>
<p>Hello!</p>
<p>My name is...</p>
</blockquote>
_transformSubscriptAndSuperscriptTags(text) → {string}
Look at the example below to see what exactly this method replaces.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
H.{2}O. 3^{2}=9 => H<sub>2</sub>O. 3<sup>2</sup>=9
_transformSubstitutions(text) → {string}
Look at the example below to see what exactly this method does.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
// Transforms the text:
{{rf=Russian Federation}}
I'm from {{rf}}
// into:
I'm from Russian Federation
_transformTables(text) → {string}
Look at the example below to see what exactly this method does.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
// Transforms the text:
[table]
[row][header]h1[/header][header]h2[/header][/row]
[row][cell]1[/cell][cell]2[/cell][/row]
[row][cell]3[/cell][cell]4[/cell][/row]
[/table]
// into:
<table>
<tr><th>h1</th><th>h2</th></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>3</td><td>4</td></tr>
</table>
_transformTypographicalCharacters(text) → {string}
Look at the example below to see what exactly this method replaces.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string
Example
-- => — (—)
<< => « («)
>> => » (»)
(c) => © (©)
(r) => ® (®)
(tm) => ™ (™)
toHTML(text) → {string}
Usually, you should use this method to convert your text to HTML. It executes all transformations determined in the constructor.
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
Returns:
- Type
- string