From 0024f5cb21aff8f7f5fbee584f4d9720e9669802 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sat, 30 Mar 2024 20:18:25 +0800 Subject: [PATCH] feat(highlight): add an option to switch stripIndent --- lib/highlight.ts | 12 +++++++----- lib/prism.ts | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/highlight.ts b/lib/highlight.ts index 6279be0..22d4201 100644 --- a/lib/highlight.ts +++ b/lib/highlight.ts @@ -16,14 +16,11 @@ interface Options { mark?: number[]; tab?: string; wrap?: boolean; - disableStripIndent?: boolean + stripIndent?: boolean; } function highlightUtil(str: string, options: Options = {}) { if (typeof str !== 'string') throw new TypeError('str must be a string!'); - if (!options.disableStripIndent) { - str = stripIndent(str); - } const useHljs = Object.prototype.hasOwnProperty.call(options, 'hljs') ? options.hljs : false; const { @@ -32,10 +29,15 @@ function highlightUtil(str: string, options: Options = {}) { caption, mark = [], languageAttr = false, - tab + tab, + stripIndent: enableStripIndent = true } = options; let { wrap = true } = options; + if (enableStripIndent) { + str = stripIndent(str); + } + if (!hljs) { hljs = require('highlight.js'); } diff --git a/lib/prism.ts b/lib/prism.ts index 79776fe..daa0c4f 100644 --- a/lib/prism.ts +++ b/lib/prism.ts @@ -65,14 +65,11 @@ interface Options { lineNumber?: boolean; mark?: string; tab?: string; - disableStripIndent?: boolean + stripIndent?: boolean; } function PrismUtil(str: string, options: Options = {}) { if (typeof str !== 'string') throw new TypeError('str must be a string!'); - if (!options.disableStripIndent) { - str = stripIndent(str); - } const { lineNumber = true, @@ -81,9 +78,14 @@ function PrismUtil(str: string, options: Options = {}) { mark, firstLine, isPreprocess = true, - caption + caption, + stripIndent: enableStripIndent = true } = options; + if (enableStripIndent) { + str = stripIndent(str); + } + // To be consistent with highlight.js let language = lang === 'plaintext' || lang === 'none' ? 'none' : lang;