Skip to content

Commit

Permalink
Wrap extended blocks in an extended node
Browse files Browse the repository at this point in the history
  • Loading branch information
borgar committed Apr 21, 2021
1 parent 72196f8 commit 212a150
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
23 changes: 21 additions & 2 deletions src/VDOM.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { singletons } from './constants.js';

const EXTENDED_NODE = -3;
const HIDDEN_NODE = -2;
const RAW_NODE = -1;
const NODE = 0;
const ELEMENT_NODE = 1;
const RAW_NODE = -1;
const HIDDEN_NODE = -2;
const TEXT_NODE = 3;
const DOCUMENT_NODE = 9;
const COMMENT_NODE = 8;
Expand Down Expand Up @@ -138,6 +139,23 @@ export class CommentNode extends Node {
}


export class ExtendedNode extends Node {
constructor (tagName, attr) {
super();
this.nodeType = EXTENDED_NODE;
this.children = [];
}

appendChild (node) {
return appendTo(this, node);
}

toHTML () {
return this.children.map(d => d.toHTML()).join('');
}
}


export class Element extends Node {
constructor (tagName, attr) {
super();
Expand Down Expand Up @@ -225,6 +243,7 @@ export class Document extends Node {
d.ELEMENT_NODE = ELEMENT_NODE;
d.HIDDEN_NODE = HIDDEN_NODE;
d.RAW_NODE = RAW_NODE;
d.EXTENDED_NODE = EXTENDED_NODE;
d.TEXT_NODE = TEXT_NODE;
d.DOCUMENT_NODE = DOCUMENT_NODE;
d.COMMENT_NODE = COMMENT_NODE;
Expand Down
32 changes: 22 additions & 10 deletions src/textile/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
** textile flow content parser
*/
import Ribbon from '../Ribbon.js';
import { Element, TextNode, RawNode, HiddenNode, CommentNode } from '../VDOM.js';
import { Element, TextNode, RawNode, HiddenNode, CommentNode, ExtendedNode } from '../VDOM.js';
import re from '../re.js';

import { parseHtml, tokenize, parseHtmlAttr, testComment, testOpenTagBlock } from '../html.js';
Expand Down Expand Up @@ -99,13 +99,25 @@ export function parseBlock (src, options) {
src.advance(step);

if ((m = /^\.(\.?)(?:\s|(?=:))/.exec(src))) {
// FIXME: this whole copyAttr seems rather strange?
// FIXME: this whole copyAttr deal seems rather strange?
// slurp rest of block
src.advance(m[0]);

m = getBlockRe(blockType, !!m[1]).exec(src);
const isExtended = !!m[1];
m = getBlockRe(blockType, isExtended).exec(src);

const inner = src.sub(0, m[1].length);

// Extended blocks are wrapped in a container so that
// the source start/end positions make sense, and the
// relationship between the child blocks is not lost
let parentNode = root;
if (isExtended) {
parentNode = new ExtendedNode();
parentNode.setPos(outerOffs);
root.appendChild(parentNode);
}

// bq | bc | notextile | pre | h# | fn# | p | ###
if (blockType === 'bq') {
const mCite = /^:(\S+)\s+/.exec(inner);
Expand All @@ -118,33 +130,33 @@ export function parseBlock (src, options) {
attr: copyAttr(attr, { cite: 1, id: 1 }),
options: options
});
root
parentNode
.appendChild(new Element('blockquote', attr).setPos(outerOffs))
.appendChild([ new TextNode('\n'), ...par, new TextNode('\n') ]);
}

else if (blockType === 'bc') {
root
parentNode
.appendChild(new Element('pre', attr).setPos(outerOffs))
.appendChild(new Element('code', copyAttr(attr, { id: 1 })).setPos(outerOffs))
.appendChild(new RawNode(inner));
}

else if (blockType === 'notextile') {
root.appendChild(parseHtml(tokenize(inner)));
parentNode.appendChild(parseHtml(tokenize(inner)));
}

else if (blockType === '###') {
// ignore the insides
hasHidden = true;
root.appendChild(new HiddenNode(inner).setPos(outerOffs));
parentNode.appendChild(new HiddenNode(inner).setPos(outerOffs));
}

else if (blockType === 'pre') {
// I disagree with RedCloth, but agree with PHP here:
// "pre(foo#bar).. line1\n\nline2" prevents multiline preformat blocks
// ...which seems like the whole point of having an extended pre block?
root
parentNode
.appendChild(new Element('pre', attr).setPos(outerOffs))
.appendChild(new RawNode(inner));
}
Expand All @@ -160,7 +172,7 @@ export function parseBlock (src, options) {
fnLink
.appendChild(new Element('sup', subAttr).setPos(pos))
.appendChild(new TextNode(fnid));
root
parentNode
.appendChild(new Element('p', attr).setPos(pos))
.appendChild([
fnLink,
Expand All @@ -173,7 +185,7 @@ export function parseBlock (src, options) {
const par = paragraph(inner, { tag: blockType, attr, options });
// first paragraph must use outer offset
par[0].setPos(outerOffs);
root.appendChild(par);
parentNode.appendChild(par);
}

src.advance(m[0]);
Expand Down

0 comments on commit 212a150

Please sign in to comment.