@emergence-engineering/prosemirror-text-map
Advanced tools
Comparing version 0.0.3 to 0.1.2
@@ -1,1 +0,1 @@ | ||
const t=[{docPos:1,textPos:0}],o=o=>{let e="",n="",s=!1;const i=[];return o.descendants(((t,o)=>{if(t.type.isBlock){if(!s)return void(s=!0);e+=`${n}\n`,n=""}else t.isText&&(i.push({docPos:o,textPos:e.length+n.length}),n+=`${t.text}`)})),e+=n,{text:e,mapping:i.length?i:t}},e=(t,o)=>{for(let e=0;e<o.length;e++)if(t>=o[e].textPos&&(void 0===o[e+1]||t<o[e+1].textPos))return o[e].docPos+(t-o[e].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")};export{o as docToTextWithMapping,e as textPosToDocPos}; | ||
const t=[{docPos:1,textPos:0}],e=(e,o={})=>{const n=o.nodeToTextMappingOverride||{};let s="",i="",r=!1;const p=[];return e.descendants(((t,e)=>{if(t.type.name in n){const{text:o,mapping:x}=n[t.type.name](t);return r?s+=`${i}\n`:r=!0,i=o,p.push(...x.map((({docPos:t,textPos:o})=>({docPos:t+e+1,textPos:s.length+o})))),!1}if(t.type.isBlock){if(!r)return r=!0,!0;s+=`${i}\n`,i=""}else t.isText&&(p.push({docPos:e,textPos:s.length+i.length}),i+=`${t.text}`);return!0})),s+=i,{text:s,mapping:p.length?p:t}},o=(t,e)=>{for(let o=0;o<e.length;o++)if(t>=e[o].textPos&&(void 0===e[o+1]||t<e[o+1].textPos))return e[o].docPos+(t-e[o].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")};export{e as docToTextWithMapping,o as textPosToDocPos}; |
@@ -1,1 +0,1 @@ | ||
"use strict";const t=[{docPos:1,textPos:0}];exports.docToTextWithMapping=o=>{let e="",s="",n=!1;const i=[];return o.descendants(((t,o)=>{if(t.type.isBlock){if(!n)return void(n=!0);e+=`${s}\n`,s=""}else t.isText&&(i.push({docPos:o,textPos:e.length+s.length}),s+=`${t.text}`)})),e+=s,{text:e,mapping:i.length?i:t}},exports.textPosToDocPos=(t,o)=>{for(let e=0;e<o.length;e++)if(t>=o[e].textPos&&(void 0===o[e+1]||t<o[e+1].textPos))return o[e].docPos+(t-o[e].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")}; | ||
"use strict";const t=[{docPos:1,textPos:0}];exports.docToTextWithMapping=(e,o={})=>{const n=o.nodeToTextMappingOverride||{};let s="",i="",r=!1;const p=[];return e.descendants(((t,e)=>{if(t.type.name in n){const{text:o,mapping:x}=n[t.type.name](t);return r?s+=`${i}\n`:r=!0,i=o,p.push(...x.map((({docPos:t,textPos:o})=>({docPos:t+e+1,textPos:s.length+o})))),!1}if(t.type.isBlock){if(!r)return r=!0,!0;s+=`${i}\n`,i=""}else t.isText&&(p.push({docPos:e,textPos:s.length+i.length}),i+=`${t.text}`);return!0})),s+=i,{text:s,mapping:p.length?p:t}},exports.textPosToDocPos=(t,e)=>{for(let o=0;o<e.length;o++)if(t>=e[o].textPos&&(void 0===e[o+1]||t<e[o+1].textPos))return e[o].docPos+(t-e[o].textPos);throw new Error("textPositionToDocumentPosition: textPos not found in mapping")}; |
@@ -6,6 +6,12 @@ import { Node } from "prosemirror-model"; | ||
}; | ||
export declare const docToTextWithMapping: (doc: Node) => { | ||
export type TextWithMapping = { | ||
text: string; | ||
mapping: TextMappingItem[]; | ||
}; | ||
export type MappingOptions = { | ||
nodeToTextMappingOverride: { | ||
[key: string]: (node: Node) => TextWithMapping; | ||
}; | ||
}; | ||
export declare const docToTextWithMapping: (doc: Node, options?: Partial<MappingOptions>) => TextWithMapping; | ||
export declare const textPosToDocPos: (textPos: number, mapping: TextMappingItem[]) => number; |
{ | ||
"name": "@emergence-engineering/prosemirror-text-map", | ||
"version": "0.0.3", | ||
"version": "0.1.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -11,2 +11,6 @@ # prosemirror-text-map | ||
use `docToTextWithMapping` to create a mapping object & `textPosToDocPos` to map text positions to document positions. | ||
This lib is mostly helpful for situations where you use other libraries whose working on texts only (like diffing, parsing, etc.) and you need to map back the results to the ProseMirror document (like adding marks based on text change). | ||
use `docToTextWithMapping` to create a mapping object & `textPosToDocPos` to map text positions to document positions. | ||
You can use an additional mapping option which could help you to serialise a node to a different format, this could be helpful for example if you want to add images to the diff algo, to detect image (source or alt) changes too. |
6249
25
16