canvas-native
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,18 +6,9 @@ 'use strict'; | ||
}); | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })(); | ||
exports.convert = convert; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _scanner = require('../scanner'); | ||
var _scanner2 = _interopRequireDefault(_scanner); | ||
function convert(native) { | ||
var json = createGroup('canvas'); | ||
var scanner = new _scanner2['default'](native); | ||
var currentNode = json; | ||
var nodeStack = [json]; | ||
var currentNode = nodeStack[nodeStack.length - 1]; | ||
@@ -29,20 +20,34 @@ var _iteratorNormalCompletion = true; | ||
try { | ||
for (var _iterator = scanner[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _step$value = _slicedToArray(_step.value, 2); | ||
lineLoop: for (var _iterator = native[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var line = _step.value; | ||
var current = _step$value[1]; | ||
while (nodeStack.length >= 0) { | ||
if (nodeContainsLine(currentNode, line)) { | ||
appendLine(currentNode, line); | ||
continue lineLoop; | ||
} | ||
var groupType = current.groupType; | ||
if (nodeContainsNestedLine(currentNode, line)) { | ||
var newNodes = appendGroupForLine(currentNode, line); | ||
nodeStack = nodeStack.concat(newNodes); | ||
currentNode = nodeStack[nodeStack.length - 1]; | ||
appendLine(currentNode, line); | ||
continue lineLoop; | ||
} | ||
if (!nodeContainsLine(currentNode, current)) { | ||
if (groupType === 'canvas') { | ||
currentNode = json; | ||
} else { | ||
var newGroup = createGroup(groupType); | ||
if (currentNode.type === 'canvas') { | ||
var newGroup = createGroupFromLine(line); | ||
json.content.push(newGroup); | ||
currentNode = newGroup; | ||
nodeStack.push(newGroup); | ||
appendLine(currentNode, line); | ||
continue lineLoop; | ||
} | ||
currentNode = nodeStack.pop() || json; | ||
} | ||
currentNode.content.push(current.toJSON()); | ||
if (!nodeStack.length) { | ||
nodeStack = [json]; | ||
} | ||
} | ||
@@ -67,2 +72,26 @@ } catch (err) { | ||
function appendGroupForLine(node, line) { | ||
var nodeLevel = getNodeLevel(node); | ||
var lineLevel = line.level; | ||
var currentNode = node; | ||
var nodeStack = []; | ||
var i = nodeLevel; | ||
while (i < lineLevel) { | ||
var group = createGroup(line.groupType); | ||
group.meta = { level: i + 1 }; | ||
nodeStack.push(group); | ||
currentNode.content.push(group); | ||
currentNode = group; | ||
i++; | ||
} | ||
return nodeStack; | ||
} | ||
function appendLine(node, line) { | ||
node.content.push(line.toJSON()); | ||
} | ||
function createGroup(type) { | ||
@@ -75,4 +104,46 @@ return { | ||
function createGroupFromLine(line) { | ||
var group = createGroup(line.groupType); | ||
if (typeof line.level === 'number') { | ||
group.meta = { level: line.level }; | ||
} | ||
return group; | ||
} | ||
function getNodeLevel(node) { | ||
if (node.type === 'canvas') { | ||
return -1; | ||
} | ||
if (!node.meta || typeof node.meta.level !== 'number') { | ||
return Infinity; | ||
} | ||
return node.meta.level; | ||
} | ||
function nodeContainsLine(node, line) { | ||
return node.type === line.groupType; | ||
var sameType = node.type === line.groupType; | ||
if (typeof line.level === 'number') { | ||
return sameType && line.level === getNodeLevel(node); | ||
} | ||
return sameType; | ||
} | ||
function nodeContainsNestedLine(node, line) { | ||
var nodeLevel = getNodeLevel(node); | ||
if (typeof line.level !== 'number') { | ||
return false; | ||
} | ||
if (node.type !== 'canvas' && node.type !== line.groupType) { | ||
return false; | ||
} | ||
return nodeLevel < line.level; | ||
} |
@@ -1,23 +0,37 @@ | ||
import Scanner from '../scanner'; | ||
export function convert(native) { | ||
const json = createGroup('canvas'); | ||
const scanner = new Scanner(native); | ||
const json = createGroup('canvas'); | ||
let currentNode = json; | ||
let nodeStack = [json]; | ||
let currentNode = nodeStack[nodeStack.length - 1]; | ||
for (const [, current, ] of scanner) { | ||
const groupType = current.groupType; | ||
lineLoop: for (const line of native) { | ||
while (nodeStack.length >= 0) { | ||
if (nodeContainsLine(currentNode, line)) { | ||
appendLine(currentNode, line); | ||
continue lineLoop; | ||
} | ||
if (!nodeContainsLine(currentNode, current)) { | ||
if (groupType === 'canvas') { | ||
currentNode = json; | ||
} else { | ||
const newGroup = createGroup(groupType); | ||
if (nodeContainsNestedLine(currentNode, line)) { | ||
const newNodes = appendGroupForLine(currentNode, line); | ||
nodeStack = nodeStack.concat(newNodes); | ||
currentNode = nodeStack[nodeStack.length - 1]; | ||
appendLine(currentNode, line); | ||
continue lineLoop; | ||
} | ||
if (currentNode.type === 'canvas') { | ||
const newGroup = createGroupFromLine(line); | ||
json.content.push(newGroup); | ||
currentNode = newGroup; | ||
nodeStack.push(newGroup); | ||
appendLine(currentNode, line); | ||
continue lineLoop; | ||
} | ||
currentNode = nodeStack.pop() || json; | ||
} | ||
currentNode.content.push(current.toJSON()); | ||
if (!nodeStack.length) { | ||
nodeStack = [json]; | ||
} | ||
} | ||
@@ -28,2 +42,26 @@ | ||
function appendGroupForLine(node, line) { | ||
const nodeLevel = getNodeLevel(node); | ||
const lineLevel = line.level; | ||
let currentNode = node; | ||
const nodeStack = []; | ||
let i = nodeLevel; | ||
while (i < lineLevel) { | ||
const group = createGroup(line.groupType); | ||
group.meta = { level: i + 1 }; | ||
nodeStack.push(group); | ||
currentNode.content.push(group); | ||
currentNode = group; | ||
i++; | ||
} | ||
return nodeStack; | ||
} | ||
function appendLine(node, line) { | ||
node.content.push(line.toJSON()); | ||
} | ||
function createGroup(type) { | ||
@@ -36,4 +74,46 @@ return { | ||
function createGroupFromLine(line) { | ||
const group = createGroup(line.groupType); | ||
if (typeof line.level === 'number') { | ||
group.meta = { level: line.level }; | ||
} | ||
return group; | ||
} | ||
function getNodeLevel(node) { | ||
if (node.type === 'canvas') { | ||
return -1; | ||
} | ||
if (!node.meta || typeof node.meta.level !== 'number') { | ||
return Infinity; | ||
} | ||
return node.meta.level; | ||
} | ||
function nodeContainsLine(node, line) { | ||
return node.type === line.groupType; | ||
const sameType = node.type === line.groupType; | ||
if (typeof line.level === 'number') { | ||
return sameType && line.level === getNodeLevel(node); | ||
} | ||
return sameType; | ||
} | ||
function nodeContainsNestedLine(node, line) { | ||
const nodeLevel = getNodeLevel(node); | ||
if (typeof line.level !== 'number') { | ||
return false; | ||
} | ||
if (node.type !== 'canvas' && node.type !== line.groupType) { | ||
return false; | ||
} | ||
return nodeLevel < line.level; | ||
} |
{ | ||
"name": "canvas-native", | ||
"description": "Utilities for working with the native Canvas format", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Jonathan Clem <jonathan@usecanvas.com>", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/usecanvas/canvas-native/issues", |
@@ -41,2 +41,3 @@ import { convert } from '../../../lib/converters/json'; | ||
type : 'unordered-list', | ||
meta : { level: 0 }, | ||
content: [ | ||
@@ -67,2 +68,117 @@ { | ||
it('handles nested lists', () => { | ||
const doc = parse([ | ||
`${wrap('unordered-list-1')}- UL-1-0`, | ||
`${wrap('unordered-list-2')}- UL-2-0`, | ||
`${wrap('unordered-list-2')}- UL-2-1`, | ||
`${wrap('unordered-list-3')}- UL-3-0`, | ||
`${wrap('unordered-list-2')}- UL-2-2`, | ||
`${wrap('unordered-list-0')}- UL-0-0`, | ||
`${wrap('ordered-list-2')}1. OL-2-0`, | ||
`${wrap('ordered-list-1')}1. OL-1-0`, | ||
`${wrap('unordered-list-0')}- UL-0-0`, | ||
'Paragraph' | ||
].join('\n')); | ||
expect(convert(doc).content).to.eql([ | ||
{ | ||
type : 'unordered-list', | ||
meta : { level: 0 }, | ||
content: [ | ||
{ | ||
type : 'unordered-list', | ||
meta : { level: 1 }, | ||
content: [ | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 1 }, | ||
content: '- UL-1-0' | ||
}, | ||
{ | ||
type : 'unordered-list', | ||
meta : { level: 2 }, | ||
content: [ | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 2 }, | ||
content: '- UL-2-0' | ||
}, | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 2 }, | ||
content: '- UL-2-1' | ||
}, | ||
{ | ||
type : 'unordered-list', | ||
meta : { level: 3 }, | ||
content: [ | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 3 }, | ||
content: '- UL-3-0' | ||
}, | ||
] | ||
}, | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 2 }, | ||
content: '- UL-2-2' | ||
}, | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 0 }, | ||
content: '- UL-0-0' | ||
} | ||
] | ||
}, | ||
{ | ||
type : 'ordered-list', | ||
meta : { level: 0 }, | ||
content: [ | ||
{ | ||
type : 'ordered-list', | ||
meta : { level: 1 }, | ||
content: [ | ||
{ | ||
type : 'ordered-list', | ||
meta : { level: 2 }, | ||
content: [ | ||
{ | ||
type : 'ordered-list-item', | ||
meta : { level: 2 }, | ||
content: '1. OL-2-0', | ||
}, | ||
] | ||
}, | ||
{ | ||
type : 'ordered-list-item', | ||
meta : { level: 1 }, | ||
content: '1. OL-1-0', | ||
}, | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
type: 'unordered-list', | ||
meta: { level: 0 }, | ||
content: [ | ||
{ | ||
type : 'unordered-list-item', | ||
meta : { level: 0 }, | ||
content: '- UL-0-0', | ||
} | ||
] | ||
}, | ||
{ | ||
type : 'paragraph', | ||
content: 'Paragraph', | ||
}, | ||
]); | ||
}); | ||
it('wraps code blocks', () => { | ||
@@ -69,0 +185,0 @@ const doc = parse([ |
80983
2092