Skip to content

Commit

Permalink
Merge branch 'prosemirror'
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Jun 28, 2016
2 parents b153728 + 4351bad commit b610171
Show file tree
Hide file tree
Showing 71 changed files with 1,826 additions and 1,684 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,26 @@ var content = MarkupIt.DraftUtils.decode(rawContent);
var text = markdown.toText(content);
```

#### Custom Syntax
### Extend Syntax

This module contains the [markdown syntax](./syntaxes/markdown), but you can write your custom syntax or extend the existing ones.

#### Create rules

```js
var myRule = MarkupIt.Rule(DraftMarkup.BLOCKS.HEADING_1)
.regExp(/^<h1>(\S+)<\/h1>/, function(match) {
.regExp(/^<h1>(\S+)<\/h1>/, function(state, match) {
return {
text: match[1]
tokens: state.parseAsInline(match[1])
};
})
.toText(function(innerText) {
return '<h1>' + innerText+ '</h1>';
.toText(function(state, token) {
return '<h1>' + state.renderAsInline(token) + '</h1>';
});
```

#### Custom Syntax

Create a new syntax inherited from the markdown one:

```js
Expand Down
14 changes: 14 additions & 0 deletions bin/toProseMirror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env node
/* eslint-disable no-console */

var MarkupIt = require('../');
var utils = require('./utils');

utils.command(function(content) {
console.log(
JSON.stringify(
MarkupIt.ProseMirrorUtils.encode(content),
null, 4
)
);
});
55 changes: 30 additions & 25 deletions lib/__tests__/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('Custom Syntax', function() {
var syntax = MarkupIt.Syntax('mysyntax', {
inline: [
MarkupIt.Rule(MarkupIt.STYLES.BOLD)
.regExp(/^\*\*([\s\S]+?)\*\*/, function(match) {
.regExp(/^\*\*([\s\S]+?)\*\*/, function(state, match) {
return {
text: match[1]
};
Expand All @@ -22,47 +22,52 @@ describe('Custom Syntax', function() {

it('should parse as unstyled', function() {
var content = markup.toContent('Hello World');
var tokens = content.getTokens();

tokens.size.should.equal(1);
var p = tokens.get(0);
var doc = content.getToken();
var blocks = doc.getTokens();

blocks.size.should.equal(1);
var p = blocks.get(0);

p.getType().should.equal(MarkupIt.BLOCKS.TEXT);
p.getText().should.equal('Hello World');
p.getAsPlainText().should.equal('Hello World');
});

it('should parse inline', function() {
var content = markup.toContent('Hello **World**');
var tokens = content.getTokens();
var doc = content.getToken();
var blocks = doc.getTokens();

tokens.size.should.equal(1);
var p = tokens.get(0);
blocks.size.should.equal(1);
var p = blocks.get(0);

p.getType().should.equal(MarkupIt.BLOCKS.TEXT);
p.getText().should.equal('Hello World');
p.getAsPlainText().should.equal('Hello World');
});
});

describe('.toText', function() {
it('should output correct string', function() {
var content = MarkupIt.JSONUtils.decode({
syntax: 'mysyntax',
tokens: [
{
type: MarkupIt.BLOCKS.PARAGRAPH,
text: 'Hello World',
tokens: [
{
type: MarkupIt.STYLES.TEXT,
text: 'Hello '
},
{
type: MarkupIt.STYLES.BOLD,
text: 'World'
}
]
}
]
token: {
type: MarkupIt.BLOCKS.DOCUMENT,
tokens: [
{
type: MarkupIt.BLOCKS.PARAGRAPH,
tokens: [
{
type: MarkupIt.STYLES.TEXT,
text: 'Hello '
},
{
type: MarkupIt.STYLES.BOLD,
text: 'World'
}
]
}
]
}
});
var text = markup.toText(content);
text.should.equal('Hello **World**\n');
Expand Down
39 changes: 19 additions & 20 deletions lib/constants/blocks.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
module.exports = {
IGNORE: 'ignore',
TEXT: 'text',
DOCUMENT: 'doc',
TEXT: 'unstyled',

CODE: 'code-block',
CODE: 'code_block',
BLOCKQUOTE: 'blockquote',
PARAGRAPH: 'unstyled',
PARAGRAPH: 'paragraph',
FOOTNOTE: 'footnote',
DEFINITION: 'definition',
HTML: 'html-block',
HR: 'atomic',
HTML: 'html_block',
HR: 'hr',

HEADING_1: 'header-one',
HEADING_2: 'header-two',
HEADING_3: 'header-three',
HEADING_4: 'header-four',
HEADING_5: 'header-five',
HEADING_6: 'header-six',
HEADING_1: 'header_one',
HEADING_2: 'header_two',
HEADING_3: 'header_three',
HEADING_4: 'header_four',
HEADING_5: 'header_five',
HEADING_6: 'header_six',

TABLE: 'table',
TABLE_HEADER: 'table-header',
TABLE_BODY: 'table-body',
TABLE_ROW: 'table-row',
TABLE_CELL: 'table-cell',
TABLE_ROW: 'table_row',
TABLE_CELL: 'table_cell',

OL_ITEM: 'ordered-list-item',
UL_ITEM: 'unordered-list-item',
OL_LIST: 'ordered_list',
UL_LIST: 'unordered_list',

LIST_ITEM: 'list_item',

// GitBook specifics
TEMPLATE: 'template',
MATH: 'math'
MATH: 'math_block'
};
26 changes: 23 additions & 3 deletions lib/constants/defaultRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@ var Rule = require('../models/rule');
var BLOCKS = require('./blocks');
var STYLES = require('./styles');

var defaultDocumentRule = Rule(BLOCKS.DOCUMENT)
.match(function(state, text) {
return {
tokens: state.parseAsBlock(text)
};
})
.toText(function(state, token) {
return state.renderAsBlock(token);
});

var defaultBlockRule = Rule(BLOCKS.TEXT)
.match(function(state, text) {
return {
tokens: state.parseAsInline(text)
};
})
.toText('%s\n');

var defaultInlineRule = Rule(STYLES.TEXT)
.setOption('parse', false)
.match(function(state, text) {
return {
text: text
};
})
.toText('%s');

module.exports = {
blockRule: defaultBlockRule,
inlineRule: defaultInlineRule
documentRule: defaultDocumentRule,
blockRule: defaultBlockRule,
inlineRule: defaultInlineRule
};
2 changes: 0 additions & 2 deletions lib/constants/entities.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module.exports = {
LINK: 'link',
LINK_REF: 'link-ref',
IMAGE: 'image',
LINK_IMAGE: 'link-image',
FOOTNOTE_REF: 'footnote-ref',

// GitBook specifics
Expand Down
3 changes: 2 additions & 1 deletion lib/constants/styles.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = {
TEXT: 'text',

BOLD: 'BOLD',
ITALIC: 'ITALIC',
CODE: 'CODE',
STRIKETHROUGH: 'STRIKETHROUGH',
TEXT: 'UNSTYLED',
HTML: 'HTML',

// GitBook specifics
Expand Down
78 changes: 0 additions & 78 deletions lib/draft/__tests__/decode.js

This file was deleted.

24 changes: 0 additions & 24 deletions lib/draft/__tests__/encode.js

This file was deleted.

Loading

0 comments on commit b610171

Please sign in to comment.