From ef15f12198a6f8c0e59fc4919249f0e77dd5e300 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Tue, 17 May 2022 14:04:57 -0500 Subject: [PATCH] Add makeHighlightedMarkup helper --- helpers/stork/browser.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/helpers/stork/browser.js b/helpers/stork/browser.js index 53ca230..070df7c 100644 --- a/helpers/stork/browser.js +++ b/helpers/stork/browser.js @@ -4,6 +4,46 @@ import { storkScriptURL } from '~/helpers/stork/config.js' + +export function makeHighlightedMarkup ( options = {} ) { + const { + text, + highlight_ranges, + withElipsis = true, + } = options + + if ( highlight_ranges.length === 0 ) return [ text ] + + const highlighted_text = highlight_ranges.map( range => { + const { + beginning, + end + } = range + + const before = text.slice( 0, beginning ) + const target = text.slice( beginning, end + 1 ) + const after = text.slice( end + 1 ) + + // console.log({ + // before, + // target, + // after + // }) + + // `${ highlighted_text }` + + return [ + withElipsis ? '...' : '', + before.trim(), + /* html */` ${ target.trim() } `, + after.trim(), + withElipsis ? '...' : '', + ].join('') + } ) + + return highlighted_text +} + export class StorkClient { constructor ( options = {} ) {