From 4e62f879ab61c922ecc8a97b85f0db339a2c7e76 Mon Sep 17 00:00:00 2001 From: Uiolee <22849383+uiolee@users.noreply.github.com> Date: Sun, 31 Mar 2024 20:07:44 +0800 Subject: [PATCH] feat(highlight): add an option to switch stripIndent (#398) * feat(highlight): provide an option to disable stripIndent * feat(highlight): add an option to switch stripIndent * update docs --- README.md | 3 +++ lib/highlight.ts | 9 +++++++-- lib/prism.ts | 9 +++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b0d12f2c..b8730fc4 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,7 @@ Option | Description | Default `autoDetect` | Detect language automatically (warning: slow)
_Sublanguage highlight requires `autoDetect` to be enabled and `lang` to be unset_ | false `mark` | Line highlight specific line(s) | `languageAttr` | Output code language into `data-language` attr | false +`stripIndent`| Whether to strip leading whitespace via [strip-indent](https://www.npmjs.com/package/strip-indent) | true ### htmlTag(tag, attrs, text, escape) @@ -451,6 +452,8 @@ Option | Description | Default `mark` | Highlight specific line | `firstLine` | First line number | `caption` | Caption | +`stripIndent`| Whether to strip leading whitespace via [strip-indent](https://www.npmjs.com/package/strip-indent) | true + When `isPreprocess` is enabled, `prismHighlight()` will return PrismJS processed HTML snippet. Otherwise `str` will only be escaped and `prismHighlight()` will return the HTML snippet that is suitable for `prism.js` working in the Browser. diff --git a/lib/highlight.ts b/lib/highlight.ts index 0726d0a8..22d42015 100644 --- a/lib/highlight.ts +++ b/lib/highlight.ts @@ -16,11 +16,11 @@ interface Options { mark?: number[]; tab?: string; wrap?: boolean; + stripIndent?: boolean; } function highlightUtil(str: string, options: Options = {}) { if (typeof str !== 'string') throw new TypeError('str must be a string!'); - str = stripIndent(str); const useHljs = Object.prototype.hasOwnProperty.call(options, 'hljs') ? options.hljs : false; const { @@ -29,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 efab6ea9..daa0c4f6 100644 --- a/lib/prism.ts +++ b/lib/prism.ts @@ -65,11 +65,11 @@ interface Options { lineNumber?: boolean; mark?: string; tab?: string; + stripIndent?: boolean; } function PrismUtil(str: string, options: Options = {}) { if (typeof str !== 'string') throw new TypeError('str must be a string!'); - str = stripIndent(str); const { lineNumber = true, @@ -78,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;