ansi-to-react
Advanced tools
Comparing version 6.0.7 to 6.0.8
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -16,3 +28,2 @@ const anser_1 = __importDefault(require("anser")); | ||
const React = __importStar(require("react")); | ||
const LINK_REGEX = /^(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})$/; | ||
/** | ||
@@ -89,25 +100,26 @@ * Converts ANSI strings into JSON output. | ||
} | ||
const content = bundle.content | ||
.split(/(\s+)/) | ||
.reduce((words, word, index) => { | ||
// If this is a separator, re-add the space removed from split. | ||
if (index % 2 === 1) { | ||
words.push(word); | ||
return words; | ||
const content = []; | ||
const linkRegex = /(\s+|^)(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})(\s+|$)/g; | ||
let index = 0; | ||
let match; | ||
while ((match = linkRegex.exec(bundle.content)) !== null) { | ||
const [, pre, url, post] = match; | ||
const startIndex = match.index + pre.length; | ||
if (startIndex > index) { | ||
content.push(bundle.content.substring(index, startIndex)); | ||
} | ||
// If this isn't a link, just return the word as-is. | ||
if (!LINK_REGEX.test(word)) { | ||
words.push(word); | ||
return words; | ||
} | ||
// Make sure the href we generate from the link is fully qualified. We assume http | ||
// if it starts with a www because many sites don't support https | ||
const href = word.startsWith("www.") ? `http://${word}` : word; | ||
words.push(React.createElement("a", { | ||
const href = url.startsWith("www.") ? `http://${url}` : url; | ||
content.push(React.createElement("a", { | ||
key: index, | ||
href, | ||
target: "_blank" | ||
}, `${word}`)); | ||
return words; | ||
}, []); | ||
}, `${url}`)); | ||
const endIndex = linkRegex.lastIndex - post.length; | ||
index = endIndex; | ||
} | ||
if (index < bundle.content.length) { | ||
content.push(bundle.content.substring(index)); | ||
} | ||
return React.createElement("span", { style, key, className }, content); | ||
@@ -114,0 +126,0 @@ } |
{ | ||
"name": "ansi-to-react", | ||
"version": "6.0.7", | ||
"version": "6.0.8", | ||
"description": "ANSI to React Elements", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,4 +5,2 @@ import Anser, { AnserJsonEntry } from "anser"; | ||
const LINK_REGEX = /^(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})$/; | ||
/** | ||
@@ -106,33 +104,38 @@ * Converts ANSI strings into JSON output. | ||
const content = bundle.content | ||
.split(/(\s+)/) | ||
.reduce((words: React.ReactNode[], word: string, index: number) => { | ||
// If this is a separator, re-add the space removed from split. | ||
if (index % 2 === 1) { | ||
words.push(word); | ||
return words; | ||
} | ||
const content: React.ReactNode[] = []; | ||
const linkRegex = /(\s+|^)(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})(\s+|$)/g; | ||
// If this isn't a link, just return the word as-is. | ||
if (!LINK_REGEX.test(word)) { | ||
words.push(word); | ||
return words; | ||
} | ||
let index = 0; | ||
let match: RegExpExecArray | null; | ||
while ((match = linkRegex.exec(bundle.content)) !== null) { | ||
const [ , pre, url, post ] = match; | ||
// Make sure the href we generate from the link is fully qualified. We assume http | ||
// if it starts with a www because many sites don't support https | ||
const href = word.startsWith("www.") ? `http://${word}` : word; | ||
words.push( | ||
React.createElement( | ||
"a", | ||
{ | ||
key: index, | ||
href, | ||
target: "_blank" | ||
}, | ||
`${word}` | ||
) | ||
); | ||
return words; | ||
}, [] as React.ReactNode[]); | ||
const startIndex = match.index + pre.length; | ||
if (startIndex > index) { | ||
content.push(bundle.content.substring(index, startIndex)); | ||
} | ||
// Make sure the href we generate from the link is fully qualified. We assume http | ||
// if it starts with a www because many sites don't support https | ||
const href = url.startsWith("www.") ? `http://${url}` : url; | ||
content.push( | ||
React.createElement( | ||
"a", | ||
{ | ||
key: index, | ||
href, | ||
target: "_blank" | ||
}, | ||
`${url}` | ||
) | ||
); | ||
const endIndex = linkRegex.lastIndex - post.length; | ||
index = endIndex; | ||
} | ||
if (index < bundle.content.length) { | ||
content.push(bundle.content.substring(index)); | ||
} | ||
return React.createElement("span", { style, key, className }, content); | ||
@@ -139,0 +142,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
189630
530