react-marksome
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -10,6 +10,6 @@ 'use strict'; | ||
// Capture [text][reference] or [reference] (taking into account escaped squared brackets) | ||
const REFERENCE_LINK_REGEXP = /(?<!\\)(?:\\\\)*(?:\[(.+?)(?<!\\)(?:\\\\)*\])?\[((?:(?<!\\)(?:\\\\)*\\[[]|[^[])+?)(?<!\\)(?:\\\\)*\]/g; // Capture sequence of '*' or '_' (non-escaped) | ||
// Capture [text][reference] or [reference] | ||
const REFERENCE_LINK_REGEXP = /(?:\[(.+?)\])?\[([^[\]]+)?\]/g; // Capture sequence of '*' or '_' | ||
const EMPH_SEQUENCE_REGEXP = /(?<!\\)(?:\\\\)*(\*+|_+)/g; | ||
const EMPH_SEQUENCE_REGEXP = /(\*+|_+)/g; | ||
function parseSegments(text) { | ||
@@ -21,5 +21,26 @@ const matches = []; | ||
const reference = referenceLinkRegExpMatch[2]; | ||
const startIndex = referenceLinkRegExpMatch.index; | ||
const innerText = referenceLinkRegExpMatch[1] || reference; | ||
let startIndex = referenceLinkRegExpMatch.index; | ||
const endIndex = startIndex + referenceLinkRegExpMatch[0].length; | ||
let innerText = referenceLinkRegExpMatch[1]; | ||
let referenceBlockStartIndex; | ||
if (innerText) { | ||
// it was matched as a shortcut reference link `[innerText][reference]` | ||
referenceBlockStartIndex = innerText.length + 2; // if the innerText is escaped -> discard the innerText block | ||
if (isCharEscaped(text, startIndex) || isCharEscaped(text, innerText.length + 1)) { | ||
startIndex = referenceBlockStartIndex; | ||
innerText = reference; | ||
} | ||
} else { | ||
// it was matched as a shortcut reference link `[reference]` | ||
innerText = reference; | ||
referenceBlockStartIndex = startIndex; | ||
} // if the reference block is escaped -> discard this whole match | ||
if (isCharEscaped(text, referenceBlockStartIndex) || isCharEscaped(text, endIndex - 1)) { | ||
return; | ||
} | ||
const match = { | ||
@@ -39,5 +60,16 @@ type: 'reference-link', | ||
matchAll(EMPH_SEQUENCE_REGEXP, text, emphCharRegExpMatch => { | ||
let index = emphCharRegExpMatch.index; | ||
let length = emphCharRegExpMatch[0].length; // if the first char of the sequence is escaped | ||
if (isCharEscaped(text, index)) { | ||
if (length === 1) { | ||
return; | ||
} // ignore the escaped char | ||
index++; | ||
length--; | ||
} | ||
const char = emphCharRegExpMatch[1][0]; | ||
const length = emphCharRegExpMatch[0].length; | ||
const index = emphCharRegExpMatch.index; | ||
const previousCharInfo = getCharInfo(text[index - 1]); | ||
@@ -235,2 +267,13 @@ const nextCharInfo = getCharInfo(text[index + length]); | ||
function isCharEscaped(text, charIndex) { | ||
let isEscaped = false; | ||
let currentCharIndex = charIndex; | ||
while (text[--currentCharIndex] === '\\') { | ||
isEscaped = !isEscaped; | ||
} | ||
return isEscaped; | ||
} | ||
function unescapeText(text) { | ||
@@ -237,0 +280,0 @@ // subset of escapable markdown chars which are used as markers in this lib |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e;const r=/(?<!\\)(?:\\\\)*(?:\[(.+?)(?<!\\)(?:\\\\)*\])?\[((?:(?<!\\)(?:\\\\)*\\[[]|[^[])+?)(?<!\\)(?:\\\\)*\]/g,s=/(?<!\\)(?:\\\\)*(\*+|_+)/g;function c(e){const t=[],n=new Map([[-1,[]]]),c=[];o(r,e,e=>{const r=e[2],s=e.index,o={type:"reference-link",innerText:e[1]||r,reference:r,startIndex:s,endIndex:s+e[0].length,offset:1};n.set(c.length,[]),c.push(o),t.push(o)});let u=0;return o(s,e,r=>{const s=r[1][0],o=r[0].length,f=r.index,a=l(e[f-1]),d=l(e[f+o]),h=!d||"."===d&&!!a,p=!a||"."===a&&!!d;let x=h,g=p;if("_"===s&&(x=h&&(!p||"."===a),g=p&&(!h||"."===d)),!x&&!g)return;let y=-1;for(;u<c.length;u++){const e=c[u];if(e.startIndex>f)break;if(!(e.endIndex<=f)){if(e.startIndex+e.offset+e.innerText.length<f)return void u++;y=u;break}}const I={char:s,index:f,length:o,type:x&&g?"<>":g?">":"<"},m=n.get(y);if("<"!==I.type)for(let n=m.length-1;n>=0;n--){const r=m[n];if(r.char!==I.char)continue;if(("<>"===r.type||"<>"===I.type)&&(r.length+I.length)%3==0&&(r.length%3||I.length%3))continue;I.type=">";let s=Math.min(I.length,r.length);for(;s>1;)t.push(i(e,"strong",r,I)),s-=2;if(s&&t.push(i(e,"emphasis",r,I)),r.length)m.splice(n+1);else if(m.splice(n),I.length)continue;break}">"!==I.type&&m.push(I)}),t.sort((e,t)=>e.startIndex-t.startIndex),function e(t,n){if(!n.length)return[f(t)];const r=n[0].startIndex,s=r>0?[f(t.slice(0,r))]:[];for(;n.length;){const r=n.shift(),c=r.startIndex+r.offset,o=[];for(;n.length&&n[0].endIndex<r.endIndex;){const e=n.shift();e.startIndex-=c,e.endIndex-=c,o.push(e)}const i=e(r.innerText,o);s.push("reference-link"===r.type?{type:r.type,content:i,reference:r.reference}:{type:r.type,content:i});const l=n.length?t.slice(r.endIndex,n[0].startIndex):t.slice(r.endIndex);l&&s.push(f(l))}return s}(e,t)}function o(e,t,n){let r;for(;null!==(r=e.exec(t));)n(r)}function i(e,t,n,r){const s=n.index+n.length,c=r.index,o="strong"===t?2:1;return n.length-=o,r.length-=o,r.index+=o,{type:t,startIndex:s-o,endIndex:c+o,innerText:e.slice(s,c),offset:o}}function l(e){return!e||/\s/.exec(e)?" ":/[!"#$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~-]/.exec(e)?".":void 0}function f(e){return e.replace(/\\([*[\\\]_])/g,"$1")}exports.Marksome=function({text:e,references:r,...s}){const o=t.useMemo(()=>c(e),[e]);return n.createElement("span",Object.assign({},s),function e(r,s){return r.map((r,c)=>{if("string"==typeof r)return r;switch(r.type){case"strong":return n.createElement("strong",{key:c},e(r.content,s));case"emphasis":return n.createElement("em",{key:c},e(r.content,s));case"reference-link":{const o=null==s?void 0:s[r.reference],i=e(r.content,s);if(!o)return n.createElement("span",{key:c},i);if("string"==typeof o)return n.createElement("a",{key:c,href:o},i);const l=o(i);return t.cloneElement(l,{key:c})}default:return null}})}(o,r))},exports.parseSegments=c; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e;const r=/(?:\[(.+?)\])?\[([^[\]]+)?\]/g,s=/(\*+|_+)/g;function c(e){const t=[],n=new Map([[-1,[]]]),c=[];o(r,e,r=>{const s=r[2];let o=r.index;const i=o+r[0].length;let l,u=r[1];if(u?(l=u.length+2,(f(e,o)||f(e,u.length+1))&&(o=l,u=s)):(u=s,l=o),f(e,l)||f(e,i-1))return;const a={type:"reference-link",innerText:u,reference:s,startIndex:o,endIndex:i,offset:1};n.set(c.length,[]),c.push(a),t.push(a)});let a=0;return o(s,e,r=>{let s=r.index,o=r[0].length;if(f(e,s)){if(1===o)return;s++,o--}const u=r[1][0],h=l(e[s-1]),d=l(e[s+o]),p=!d||"."===d&&!!h,g=!h||"."===h&&!!d;let x=p,y=g;if("_"===u&&(x=p&&(!g||"."===h),y=g&&(!p||"."===d)),!x&&!y)return;let I=-1;for(;a<c.length;a++){const e=c[a];if(e.startIndex>s)break;if(!(e.endIndex<=s)){if(e.startIndex+e.offset+e.innerText.length<s)return void a++;I=a;break}}const m={char:u,index:s,length:o,type:x&&y?"<>":y?">":"<"},k=n.get(I);if("<"!==m.type)for(let n=k.length-1;n>=0;n--){const r=k[n];if(r.char!==m.char)continue;if(("<>"===r.type||"<>"===m.type)&&(r.length+m.length)%3==0&&(r.length%3||m.length%3))continue;m.type=">";let s=Math.min(m.length,r.length);for(;s>1;)t.push(i(e,"strong",r,m)),s-=2;if(s&&t.push(i(e,"emphasis",r,m)),r.length)k.splice(n+1);else if(k.splice(n),m.length)continue;break}">"!==m.type&&k.push(m)}),t.sort((e,t)=>e.startIndex-t.startIndex),function e(t,n){if(!n.length)return[u(t)];const r=n[0].startIndex,s=r>0?[u(t.slice(0,r))]:[];for(;n.length;){const r=n.shift(),c=r.startIndex+r.offset,o=[];for(;n.length&&n[0].endIndex<r.endIndex;){const e=n.shift();e.startIndex-=c,e.endIndex-=c,o.push(e)}const i=e(r.innerText,o);s.push("reference-link"===r.type?{type:r.type,content:i,reference:r.reference}:{type:r.type,content:i});const l=n.length?t.slice(r.endIndex,n[0].startIndex):t.slice(r.endIndex);l&&s.push(u(l))}return s}(e,t)}function o(e,t,n){let r;for(;null!==(r=e.exec(t));)n(r)}function i(e,t,n,r){const s=n.index+n.length,c=r.index,o="strong"===t?2:1;return n.length-=o,r.length-=o,r.index+=o,{type:t,startIndex:s-o,endIndex:c+o,innerText:e.slice(s,c),offset:o}}function l(e){return!e||/\s/.exec(e)?" ":/[!"#$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~-]/.exec(e)?".":void 0}function f(e,t){let n=!1,r=t;for(;"\\"===e[--r];)n=!n;return n}function u(e){return e.replace(/\\([*[\\\]_])/g,"$1")}exports.Marksome=function({text:e,references:r,...s}){const o=t.useMemo(()=>c(e),[e]);return n.createElement("span",Object.assign({},s),function e(r,s){return r.map((r,c)=>{if("string"==typeof r)return r;switch(r.type){case"strong":return n.createElement("strong",{key:c},e(r.content,s));case"emphasis":return n.createElement("em",{key:c},e(r.content,s));case"reference-link":{const o=null==s?void 0:s[r.reference],i=e(r.content,s);if(!o)return n.createElement("span",{key:c},i);if("string"==typeof o)return n.createElement("a",{key:c,href:o},i);const l=o(i);return t.cloneElement(l,{key:c})}default:return null}})}(o,r))},exports.parseSegments=c; | ||
//# sourceMappingURL=react-marksome.cjs.production.min.js.map |
import React, { useMemo, cloneElement } from 'react'; | ||
// Capture [text][reference] or [reference] (taking into account escaped squared brackets) | ||
const REFERENCE_LINK_REGEXP = /(?<!\\)(?:\\\\)*(?:\[(.+?)(?<!\\)(?:\\\\)*\])?\[((?:(?<!\\)(?:\\\\)*\\[[]|[^[])+?)(?<!\\)(?:\\\\)*\]/g; // Capture sequence of '*' or '_' (non-escaped) | ||
// Capture [text][reference] or [reference] | ||
const REFERENCE_LINK_REGEXP = /(?:\[(.+?)\])?\[([^[\]]+)?\]/g; // Capture sequence of '*' or '_' | ||
const EMPH_SEQUENCE_REGEXP = /(?<!\\)(?:\\\\)*(\*+|_+)/g; | ||
const EMPH_SEQUENCE_REGEXP = /(\*+|_+)/g; | ||
function parseSegments(text) { | ||
@@ -13,5 +13,26 @@ const matches = []; | ||
const reference = referenceLinkRegExpMatch[2]; | ||
const startIndex = referenceLinkRegExpMatch.index; | ||
const innerText = referenceLinkRegExpMatch[1] || reference; | ||
let startIndex = referenceLinkRegExpMatch.index; | ||
const endIndex = startIndex + referenceLinkRegExpMatch[0].length; | ||
let innerText = referenceLinkRegExpMatch[1]; | ||
let referenceBlockStartIndex; | ||
if (innerText) { | ||
// it was matched as a shortcut reference link `[innerText][reference]` | ||
referenceBlockStartIndex = innerText.length + 2; // if the innerText is escaped -> discard the innerText block | ||
if (isCharEscaped(text, startIndex) || isCharEscaped(text, innerText.length + 1)) { | ||
startIndex = referenceBlockStartIndex; | ||
innerText = reference; | ||
} | ||
} else { | ||
// it was matched as a shortcut reference link `[reference]` | ||
innerText = reference; | ||
referenceBlockStartIndex = startIndex; | ||
} // if the reference block is escaped -> discard this whole match | ||
if (isCharEscaped(text, referenceBlockStartIndex) || isCharEscaped(text, endIndex - 1)) { | ||
return; | ||
} | ||
const match = { | ||
@@ -31,5 +52,16 @@ type: 'reference-link', | ||
matchAll(EMPH_SEQUENCE_REGEXP, text, emphCharRegExpMatch => { | ||
let index = emphCharRegExpMatch.index; | ||
let length = emphCharRegExpMatch[0].length; // if the first char of the sequence is escaped | ||
if (isCharEscaped(text, index)) { | ||
if (length === 1) { | ||
return; | ||
} // ignore the escaped char | ||
index++; | ||
length--; | ||
} | ||
const char = emphCharRegExpMatch[1][0]; | ||
const length = emphCharRegExpMatch[0].length; | ||
const index = emphCharRegExpMatch.index; | ||
const previousCharInfo = getCharInfo(text[index - 1]); | ||
@@ -227,2 +259,13 @@ const nextCharInfo = getCharInfo(text[index + length]); | ||
function isCharEscaped(text, charIndex) { | ||
let isEscaped = false; | ||
let currentCharIndex = charIndex; | ||
while (text[--currentCharIndex] === '\\') { | ||
isEscaped = !isEscaped; | ||
} | ||
return isEscaped; | ||
} | ||
function unescapeText(text) { | ||
@@ -229,0 +272,0 @@ // subset of escapable markdown chars which are used as markers in this lib |
{ | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "repository": "github:miguel-silva/react-marksome", |
@@ -40,7 +40,7 @@ export type Segment = InlineStyleSegment | ReferenceLinkSegment | string; | ||
// Capture [text][reference] or [reference] (taking into account escaped squared brackets) | ||
const REFERENCE_LINK_REGEXP = /(?<!\\)(?:\\\\)*(?:\[(.+?)(?<!\\)(?:\\\\)*\])?\[((?:(?<!\\)(?:\\\\)*\\[[]|[^[])+?)(?<!\\)(?:\\\\)*\]/g; | ||
// Capture [text][reference] or [reference] | ||
const REFERENCE_LINK_REGEXP = /(?:\[(.+?)\])?\[([^[\]]+)?\]/g; | ||
// Capture sequence of '*' or '_' (non-escaped) | ||
const EMPH_SEQUENCE_REGEXP = /(?<!\\)(?:\\\\)*(\*+|_+)/g; | ||
// Capture sequence of '*' or '_' | ||
const EMPH_SEQUENCE_REGEXP = /(\*+|_+)/g; | ||
@@ -58,8 +58,34 @@ export function parseSegments(text: string): Segment[] { | ||
const reference = referenceLinkRegExpMatch[2]; | ||
const startIndex = referenceLinkRegExpMatch.index; | ||
let startIndex = referenceLinkRegExpMatch.index; | ||
const endIndex = startIndex + referenceLinkRegExpMatch[0].length; | ||
const innerText = referenceLinkRegExpMatch[1] || reference; | ||
let innerText = referenceLinkRegExpMatch[1]; | ||
let referenceBlockStartIndex: number; | ||
const endIndex = startIndex + referenceLinkRegExpMatch[0].length; | ||
if (innerText) { | ||
// it was matched as a shortcut reference link `[innerText][reference]` | ||
referenceBlockStartIndex = innerText.length + 2; | ||
// if the innerText is escaped -> discard the innerText block | ||
if ( | ||
isCharEscaped(text, startIndex) || | ||
isCharEscaped(text, innerText.length + 1) | ||
) { | ||
startIndex = referenceBlockStartIndex; | ||
innerText = reference; | ||
} | ||
} else { | ||
// it was matched as a shortcut reference link `[reference]` | ||
innerText = reference; | ||
referenceBlockStartIndex = startIndex; | ||
} | ||
// if the reference block is escaped -> discard this whole match | ||
if ( | ||
isCharEscaped(text, referenceBlockStartIndex) || | ||
isCharEscaped(text, endIndex - 1) | ||
) { | ||
return; | ||
} | ||
const match: ReferenceLinkMatch = { | ||
@@ -83,8 +109,18 @@ type: 'reference-link', | ||
matchAll(EMPH_SEQUENCE_REGEXP, text, (emphCharRegExpMatch) => { | ||
const char = emphCharRegExpMatch[1][0] as '*' | '_'; | ||
let index = emphCharRegExpMatch.index; | ||
let length = emphCharRegExpMatch[0].length; | ||
const length = emphCharRegExpMatch[0].length; | ||
// if the first char of the sequence is escaped | ||
if (isCharEscaped(text, index)) { | ||
if (length === 1) { | ||
return; | ||
} | ||
const index = emphCharRegExpMatch.index; | ||
// ignore the escaped char | ||
index++; | ||
length--; | ||
} | ||
const char = emphCharRegExpMatch[1][0] as '*' | '_'; | ||
const previousCharInfo = getCharInfo(text[index - 1]); | ||
@@ -342,2 +378,14 @@ const nextCharInfo = getCharInfo(text[index + length]); | ||
function isCharEscaped(text: string, charIndex: number): boolean { | ||
let isEscaped = false; | ||
let currentCharIndex = charIndex; | ||
while (text[--currentCharIndex] === '\\') { | ||
isEscaped = !isEscaped; | ||
} | ||
return isEscaped; | ||
} | ||
function unescapeText(text: string) { | ||
@@ -344,0 +392,0 @@ // subset of escapable markdown chars which are used as markers in this lib |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
111650
972