Skip to content

Commit d9a3585

Browse files
committed
Merge branch '6.x.x'
2 parents 80e2fb9 + 8f857cd commit d9a3585

File tree

11 files changed

+145
-14
lines changed

11 files changed

+145
-14
lines changed

HISTORY.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
### Unreleased
77

8+
### 7.0.3
9+
10+
Import fixes from 6.0.6:
11+
12+
- Fix empty custom block serialization
13+
- Fix invalid custom block parsing
14+
815
### 7.0.2
916

1017
Import fixes from 6.0.2, 6.0.3, 6.0.4 and 6.0.5:
@@ -22,9 +29,14 @@ Import fixes from 6.0.2, 6.0.3, 6.0.4 and 6.0.5:
2229

2330
- Upgrade Slate to 0.33
2431

32+
### 6.0.6
33+
34+
- Fix empty custom block serialization
35+
- Fix invalid custom block parsing
36+
2537
### 6.0.5
2638

27-
- Fix empty custom block parsing
39+
- Fix empty custom block tag parsing
2840

2941
### 6.0.4
3042

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"trim-trailing-lines": "^1.1.0",
6666
"type-of": "^2.0.1",
6767
"uid": "0.0.2",
68+
"warning": "^3.0.0",
6869
"yargs": "^4.7.1"
6970
},
7071
"bin": {

src/markdown/blocks/custom.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const { List } = require('immutable');
2+
const warning = require('warning');
23
const trimTrailingLines = require('trim-trailing-lines');
3-
const { Serializer, Deserializer, Block } = require('../../');
4+
const { Serializer, Deserializer, Block, BLOCKS } = require('../../');
45
const reBlock = require('../re/block');
56
const liquid = require('../liquid');
67

8+
79
/**
810
* Return true if a block type is a custom one.
911
* @param {String} tag
@@ -49,6 +51,18 @@ function isClosingTagFor(tag, forTag) {
4951
return tag.indexOf(`end${forTag}`) === 0;
5052
}
5153

54+
/**
55+
* Wrap the given nodes in the default block
56+
* @param {Array<Node>} nodes
57+
* @return {Block}
58+
*/
59+
function wrapInDefaultBlock(nodes) {
60+
return Block.create({
61+
type: BLOCKS.DEFAULT,
62+
nodes
63+
});
64+
}
65+
5266
/**
5367
* Serialize a templating block to markdown
5468
* @type {Serializer}
@@ -64,24 +78,41 @@ const serialize = Serializer()
6478
data
6579
});
6680

67-
const unendingTags = state.getProp('unendingTags') || List();
68-
const endTag =
69-
unendingTags.includes(getTagFromCustomType(node.type))
70-
? ''
71-
: liquid.stringifyTag({
72-
tag: 'end' + getTagFromCustomType(node.type)
73-
});
74-
7581
const split = node.object == 'block' ? '\n' : '';
7682
const end = node.object == 'block' ? '\n\n' : '';
7783

78-
if (node.isVoid) {
84+
if (node.isVoid || node.nodes.isEmpty()) {
85+
warning(
86+
node.isVoid,
87+
'Encountered a non-void custom block with no children'
88+
);
89+
7990
return state
8091
.shift()
8192
.write(`${startTag}${end}`);
8293
}
8394

84-
const inner = trimTrailingLines(state.serialize(node.nodes));
95+
const containsInline = node.nodes.first().object !== 'block';
96+
warning(
97+
!containsInline,
98+
'Encountered a custom block containing inlines'
99+
);
100+
101+
const innerNodes = containsInline
102+
? List([wrapInDefaultBlock(node.nodes)])
103+
: node.nodes;
104+
105+
const inner = trimTrailingLines(
106+
state.serialize(innerNodes)
107+
);
108+
109+
const unendingTags = state.getProp('unendingTags') || List();
110+
const endTag =
111+
unendingTags.includes(getTagFromCustomType(node.type))
112+
? ''
113+
: liquid.stringifyTag({
114+
tag: 'end' + getTagFromCustomType(node.type)
115+
});
85116

86117
return state
87118
.shift()
@@ -103,7 +134,15 @@ const deserialize = Deserializer()
103134
return state;
104135
}
105136

137+
<<<<<<< HEAD
106138
const { tag, data } = liquid.parseTag(text);
139+
=======
140+
const parsed = liquid.parseTag(text);
141+
if (!parsed) {
142+
return state;
143+
}
144+
const { tag, data } = parsed;
145+
>>>>>>> 6.x.x
107146

108147
const node = Block.create({
109148
type: getCustomTypeFromTag(tag),

src/markdown/liquid/parseTag.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ function parseData(text) {
5151
/**
5252
* Parse the inner text of a tag.
5353
* @param {String} text
54-
* @return {Object} { tag: String, data: Map }
54+
* @return {Object | Null} { tag: String, data: Map }
5555
*/
5656
function parseTag(text) {
5757
const match = text.match(lexical.tagLine);
5858

59+
if (!match) {
60+
return null;
61+
}
62+
5963
return {
6064
tag: match[1],
6165
data: parseData(match[2])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
<<<<<<< HEAD
12
Hello world
23

34
{% %}
45

56
{% %}
67

8+
=======
9+
A
10+
11+
{% %}
12+
13+
B
14+
15+
{% %}
16+
17+
C
18+
19+
>>>>>>> 6.x.x
720
{% This one is parsed as a paragraph
821
%}

test/from-markdown/custom-blocks/empty-tag/output.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@ document:
55
nodes:
66
- object: text
77
leaves:
8-
- text: Hello world
8+
- text: A
9+
- object: block
10+
type: paragraph
11+
nodes:
12+
- object: text
13+
leaves:
14+
- text: B
15+
- object: block
16+
type: paragraph
17+
nodes:
18+
- object: text
19+
leaves:
20+
- text: C
921
- object: block
1022
type: paragraph
1123
nodes:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
A
2+
3+
{% !! %}
4+
5+
B
6+
7+
{% =# %}
8+
9+
C
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
document:
2+
nodes:
3+
- object: block
4+
type: paragraph
5+
nodes:
6+
- object: text
7+
leaves:
8+
- text: A
9+
- object: block
10+
type: paragraph
11+
nodes:
12+
- object: text
13+
leaves:
14+
- text: B
15+
- object: block
16+
type: paragraph
17+
nodes:
18+
- object: text
19+
leaves:
20+
- text: C
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
document:
2+
nodes:
3+
- object: block
4+
type: x-empty
5+
# Custom blocks should either contain blocks or be Void. It's must be
6+
# ensured uphill by normalization,
7+
# This test shows that markup-it still handles non-Void empty blocks well.
8+
isVoid: false
9+
nodes:
10+
- object: text
11+
leaves:
12+
- text: ''
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% empty %}
2+
3+
{% endempty %}

0 commit comments

Comments
 (0)