@sanity/block-tools
Advanced tools
Comparing version 0.124.0-alpha.d5ac0a90 to 0.124.0-alpha.f0625b40
@@ -122,4 +122,2 @@ 'use strict'; | ||
// Trim whitespace from the DOM that is between HTML-elements and | ||
// become appended to block spans in the wrong places | ||
function trimWhitespace(blocks) { | ||
@@ -157,5 +155,2 @@ blocks.forEach(function (block) { | ||
}); | ||
if (block.children.length === 0) { | ||
block.children.push({ _type: 'span', marks: [], text: '' }); | ||
} | ||
}); | ||
@@ -162,0 +157,0 @@ return blocks; |
@@ -26,2 +26,8 @@ 'use strict'; | ||
/** | ||
* A internal variable to keep track of annotation mark definitions within the 'run' of a block | ||
* | ||
*/ | ||
var _markDefsWithinBlock = []; | ||
/** | ||
* HTML Deserializer | ||
@@ -63,5 +69,2 @@ * | ||
}; | ||
this._blocks = []; | ||
this._currentBlockChildren = []; | ||
this._currentBlockMarkDefs = []; | ||
} | ||
@@ -135,7 +138,2 @@ | ||
nodes.push(node); | ||
if (node._type === 'block') { | ||
_this._blocks.push(node); | ||
} else { | ||
_this._currentBlockChildren.push(node); | ||
} | ||
break; | ||
@@ -181,8 +179,3 @@ default: | ||
} | ||
var ret = rule.deserialize(element, next, { | ||
blocks: _this._blocks, | ||
currentBlockChildren: _this._currentBlockChildren, | ||
currentBlockMarkDefs: _this._currentBlockMarkDefs, | ||
deserialize: _this.deserializeElements | ||
}); | ||
var ret = rule.deserialize(element, next); | ||
var type = (0, _resolveJsType2.default)(ret); | ||
@@ -204,6 +197,5 @@ | ||
node = _this.deserializeAnnotation(ret); | ||
} else if (ret._type === 'block' && _this._currentBlockMarkDefs.length) { | ||
ret.markDefs = _this._currentBlockMarkDefs; | ||
_this._currentBlockMarkDefs = []; // Reset here | ||
_this._currentBlockChildren = []; | ||
} else if (ret._type === 'block' && _markDefsWithinBlock.length) { | ||
ret.markDefs = _markDefsWithinBlock; | ||
_markDefsWithinBlock = []; // Reset here | ||
node = ret; | ||
@@ -283,3 +275,3 @@ } else { | ||
_this._currentBlockMarkDefs.push(markDef); | ||
_markDefsWithinBlock.push(markDef); | ||
var applyAnnotation = function applyAnnotation(node) { | ||
@@ -286,0 +278,0 @@ if (node._type === '__annotation') { |
@@ -52,4 +52,4 @@ "use strict"; | ||
var tags = elementMap[mappedElm.className]; | ||
var text = doc.createTextNode(mappedElm.textContent); | ||
var parentElement = doc.createElement(tags[0]); | ||
var text = new Text(mappedElm.textContent); | ||
var parentElement = document.createElement(tags[0]); | ||
var parent = parentElement; | ||
@@ -56,0 +56,0 @@ var child = parentElement; |
@@ -11,6 +11,2 @@ 'use strict'; | ||
var _randomKey = require('../../util/randomKey'); | ||
var _randomKey2 = _interopRequireDefault(_randomKey); | ||
var _constants = require('../../constants'); | ||
@@ -20,16 +16,11 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// https://gist.github.com/webtobesocial/ac9d052595b406d5a5c1 | ||
function isNormalEmptyParagraph(el) { | ||
return (0, _helpers.tagName)(el) === 'p' && el.textContent === '' && el.textContent === '' && el.className === 'MsoNormal'; | ||
} | ||
function getListItemStyle(el) { | ||
var symbol = el.textContent.trim(); | ||
if (symbol.match(/\b\./)) { | ||
return 'number'; | ||
var style = void 0; | ||
if (style = el.getAttribute('style')) { | ||
if (!style.match(/lfo\d+/)) { | ||
return undefined; | ||
} | ||
return style.match('lfo1') ? 'bullet' : 'number'; | ||
} | ||
return 'bullet'; | ||
return undefined; | ||
} | ||
@@ -50,3 +41,3 @@ | ||
function isListElement(el) { | ||
function isWordListElement(el) { | ||
if (el.className) { | ||
@@ -58,34 +49,2 @@ return el.className === 'MsoListParagraphCxSpFirst' || el.className === 'MsoListParagraphCxSpMiddle' || el.className === 'MsoListParagraphCxSpLast'; | ||
function getFootnoteContentElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style === 'mso-element:footnote') { | ||
return el.getAttribute('id').trim(); | ||
} | ||
return null; | ||
} | ||
function getFootnoteLinkElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style.match(/mso-footnote-id/)) { | ||
return style.split(':')[1].trim(); | ||
} | ||
return null; | ||
} | ||
function getEndnoteContentElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style === 'mso-element:endnote') { | ||
return el.getAttribute('id').trim(); | ||
} | ||
return null; | ||
} | ||
function getEndnoteLinkElementId(el) { | ||
var style = el.getAttribute('style'); | ||
if (style && style.match(/mso-endnote-id/)) { | ||
return style.split(':')[1].trim(); | ||
} | ||
return null; | ||
} | ||
function createWordRules(blockContentType) { | ||
@@ -95,50 +54,7 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var blockNotesEnabled = options.enabledBlockAnnotations.includes('blockNote'); | ||
var blockNoteAnnotationMarker = void 0; | ||
if (blockContentType && blockNotesEnabled) { | ||
var blockType = blockContentType.of.find(function (ofType) { | ||
return ofType.name === 'block'; | ||
}); | ||
var spansType = blockType.fields.find(function (field) { | ||
return field.name === 'spans'; | ||
}).type; | ||
var annotations = spansType.of.find(function (ofType) { | ||
return ofType.name === 'span'; | ||
}).annotations; | ||
var blockNoteType = annotations.find(function (type) { | ||
return type.name === 'blockNote'; | ||
}); | ||
if (blockNoteType) { | ||
blockNoteAnnotationMarker = blockNoteType.annotationMarker; | ||
} | ||
} | ||
return [ | ||
// Fix weird paragraphing within Word (paragraph is more of a line break) | ||
// If we see two empty paragraphs after each other, we return an empty block | ||
{ | ||
return [{ | ||
deserialize: function deserialize(el, next) { | ||
if (isNormalEmptyParagraph(el)) { | ||
var nextSibling = el.nextElementSibling; | ||
if (nextSibling && isNormalEmptyParagraph(nextSibling)) { | ||
return _extends({}, _constants.DEFAULT_BLOCK, { | ||
style: 'normal', | ||
children: [{ _type: 'span', marks: [], text: '' }] | ||
}); | ||
} | ||
return next([]); | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// List elements | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
if ((0, _helpers.tagName)(el) === 'p' && isListElement(el)) { | ||
var listItem = el.querySelector("span[style='mso-list:Ignore']"); | ||
var listItemStyle = getListItemStyle(listItem); | ||
listItem.parentNode.removeChild(listItem); | ||
if ((0, _helpers.tagName)(el) === 'p' && isWordListElement(el)) { | ||
return _extends({}, _constants.DEFAULT_BLOCK, { | ||
listItem: listItemStyle, | ||
listItem: getListItemStyle(el), | ||
level: getListItemLevel(el), | ||
@@ -151,109 +67,3 @@ style: 'normal', | ||
} | ||
}, | ||
// Fotnote links | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
var footnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'a' && (footnoteId = getFootnoteLinkElementId(el))) { | ||
if (!blockNotesEnabled) { | ||
return undefined; | ||
} | ||
var markDef = { | ||
_key: (0, _randomKey2.default)(12), | ||
_type: 'blockNote', | ||
style: 'footnote', | ||
blockNoteId: footnoteId | ||
}; | ||
var children = blockNoteAnnotationMarker ? [el.ownerDocument.createTextNode(blockNoteAnnotationMarker)] : el.childNodes; | ||
return { | ||
_type: '__annotation', | ||
markDef: markDef, | ||
children: next(children) | ||
}; | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// Footnote content | ||
{ | ||
deserialize: function deserialize(el, next, _ref) { | ||
var blocks = _ref.blocks, | ||
_deserialize = _ref.deserialize; | ||
var footnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'div' && (footnoteId = getFootnoteContentElementId(el))) { | ||
if (!blockNotesEnabled) { | ||
return undefined; | ||
} | ||
// Find the block where the footnote occured | ||
var markDef = blocks.map(function (blk) { | ||
return blk.markDefs.find(function (def) { | ||
return def.blockNoteId === footnoteId; | ||
}); | ||
}).filter(Boolean)[0]; | ||
if (markDef) { | ||
el.querySelectorAll('a[name=\'_' + footnoteId + '\']').forEach(function (elm) { | ||
elm.parentNode.removeChild(elm); | ||
}); | ||
markDef.content = _deserialize(el.childNodes); | ||
delete markDef.blockNoteId; | ||
} | ||
return next([]); | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// Endnote links | ||
{ | ||
deserialize: function deserialize(el, next) { | ||
var endnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'a' && (endnoteId = getEndnoteLinkElementId(el))) { | ||
if (!blockNotesEnabled) { | ||
return undefined; | ||
} | ||
var markDef = { | ||
_key: (0, _randomKey2.default)(12), | ||
_type: 'blockNote', | ||
style: 'endnote', | ||
blockNoteId: endnoteId | ||
}; | ||
var children = blockNoteAnnotationMarker ? [el.ownerDocument.createTextNode(blockNoteAnnotationMarker)] : el.childNodes; | ||
return { | ||
_type: '__annotation', | ||
markDef: markDef, | ||
children: next(children) | ||
}; | ||
} | ||
return undefined; | ||
} | ||
}, | ||
// Endnote content | ||
{ | ||
deserialize: function deserialize(el, next, _ref2) { | ||
var blocks = _ref2.blocks, | ||
_deserialize2 = _ref2.deserialize; | ||
var endnoteId = void 0; | ||
if ((0, _helpers.tagName)(el) === 'div' && (endnoteId = getEndnoteContentElementId(el))) { | ||
if (!blockNotesEnabled) { | ||
return undefined; | ||
} | ||
// Find the block where the footnote occured | ||
var markDef = blocks.map(function (blk) { | ||
return blk.markDefs.find(function (def) { | ||
return def.blockNoteId === endnoteId; | ||
}); | ||
}).filter(Boolean)[0]; | ||
if (markDef) { | ||
el.querySelectorAll('a[name=\'_' + endnoteId + '\']').forEach(function (elm) { | ||
elm.parentNode.removeChild(elm); | ||
}); | ||
markDef.content = _deserialize2(el.childNodes); | ||
delete markDef.blockNoteId; | ||
} | ||
return next([]); | ||
} | ||
return undefined; | ||
} | ||
}]; | ||
} |
{ | ||
"name": "@sanity/block-tools", | ||
"version": "0.124.0-alpha.d5ac0a90", | ||
"version": "0.124.0-alpha.f0625b40", | ||
"description": "Can format HTML, Slate JSON or Sanity block array into any other format.", | ||
@@ -39,3 +39,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@sanity/schema": "0.124.0-alpha.d5ac0a90", | ||
"@sanity/schema": "^0.113.7", | ||
"assert": "^1.4.1", | ||
@@ -68,3 +68,3 @@ "babel-cli": "^6.24.1", | ||
"jsdom": "11.3.0", | ||
"wgxpath": "^1.2.0" | ||
"xpath": "^0.0.24" | ||
}, | ||
@@ -71,0 +71,0 @@ "directories": { |
53680
23
1290