Skip to content

Commit

Permalink
feat(highlight): add an option to switch stripIndent
Browse files Browse the repository at this point in the history
  • Loading branch information
uiolee committed Mar 30, 2024
1 parent 58ad3be commit 0024f5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions lib/highlight.ts
Expand Up @@ -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 {
Expand All @@ -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');
}
Expand Down
12 changes: 7 additions & 5 deletions lib/prism.ts
Expand Up @@ -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,
Expand All @@ -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;

Expand Down

0 comments on commit 0024f5c

Please sign in to comment.