Repository navigation
Basic Usage

Welcome to the introduction page!
Tip
The way you interact with YAMP heavily changed in v0.3.0 please consider the page history for older methods.
To start using YAMP you only need to familiarize yourself with 2 methods:
const parser = new YAMP();
parser.parse(input: string);The parse method is used to parse a markdown formatted input and return both the parsed html and a char map which maps each visible text charcter in the html back to their original markdown character.
Warning
Please make sure to sanitize your output markdown using an html sanitizer. Otherwise, it may be possible to write markdown in such a way to make it ouput mallicious content.
Arguments:
- input: string (The markdown input that should be parsed)
Returns:
-
object
- html: string (the resulting html. Sanatize this!)
-
char_map: object
-
absolute_map: Array (holds the exact position of each character in the markdown input.)
Eg:[0, 1, 2, 6, 7, 8] -
width_map: Array<Array> (holds the width of each character: 0 is a width of 1, 1 of 2 until 254. 255 means a width of 0.)
Eg[[0, 0, 0, 255, 255, 255, 0], [0, 0]] -
line_map: Array<Array> (similar to the absolute map but split accross their corresponding lines.)
Eg[[0, 1, 2, 6], [7, 8]] -
line_idx_map: Array<Array> (keeps track of the line index for each html character)
Eg[0, 0, 0, 0, 1, 1]
-
absolute_map: Array (holds the exact position of each character in the markdown input.)
Tip
line_map is available since v0.1.3
line_idx_map is available since v0.1.4
const parser = new YAMP(<options>);OR
const parser = new YAMP();
parser.set_options(<options>).This method is used to modify the options for this parser instance. Once called, it will keep those options set until it's called again. It's not recommended to set this method every time you parse. Instead set your prefered configuration once or only when changes are needed.
When the options are omitted in the constructor. The default options are used.
Arguments
- options: object (The options used to determine YAMP's behaviour)
Available Options:
In this example we display the markdown formatted equivalent of a text input directly to an ouput div. We've configured the options to only allow the specific syntax we like.
import { YAMP, Header, BlockQuote, Emphasis, Strikethrough, List, Code, Link, EscapeIncompleteHtml } as YAMP from "https://cdn.jsdelivr.net/npm/@theblackswitch/yamp@latest/dist/main.js"
import DOMPurify from 'https://cdn.jsdelivr.net/npm/dompurify/+esm';
// Find our input and output element
const text_input = document.getElementById("text_input");
const output_elem = document.getElementById("output_elem");
// Create a YAMP parser instance with out custom options
const parser = new YAMP({
"enabled_features": [
Header,
BlockQuote,
Emphasis,
Strikethrough,
List,
Code,
Link,
EscapeIncompleteHtml
],
"disable_paragraph_elements": false,
"enable_trailing_linebreaks": true,
"literal_mid_word_underscores": false,
"add_zero_width_space_for_cursor_positions": true,
"finalize_spaces": false
});
// Detect any changes from the input element and update the output render
text_input.addEventListener('input', () => {
let result = parser.parse(text_input.value);
// Don't forget to sanitize the output for security
output_elem.innerHtml = DOMPurify.sanitize(result.html);
});~ wiki by theblackswitch
{ "enabled_features": [ // Specify features here to enable them Header, // yes indeed you have to pass the class as a whole here AlternateHeader, // Make sure you don't forget to import these classes from the YAMP distributable BlockQuote, Emphasis, UnderscoreEmphasis, Strikethrough, List, Code, CodeBlock, Link, Image, HorizontalRule, Table, EscapeIncompleteHtml ], "disable_paragraph_elements": false, // Whether to disable the generation of paragraph elements "enable_trailing_linebreaks": true, // Put a linebreak at the end of each line "literal_mid_word_underscores": true, // Stop parsing underscores in the middle of a word (only used for UnderscoreEmphasis) "add_zero_width_space_for_cursor_positions": true, // Place a zero-width space for example on new lines so you're able to detect a cursor position "finalize_spaces": true, // Replace any spaces with "debug": false // Enable debug logging in the console }