Skip to content

Commit

Permalink
feat(highlight): add an option to switch stripIndent (#398)
Browse files Browse the repository at this point in the history
* feat(highlight): provide an option to disable stripIndent

* feat(highlight): add an option to switch stripIndent

* update docs
  • Loading branch information
uiolee authored Mar 31, 2024
1 parent c061251 commit 4e62f87
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ Option | Description | Default
`autoDetect` | Detect language automatically (warning: slow)<br>_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)

Expand Down Expand Up @@ -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.

Expand Down
9 changes: 7 additions & 2 deletions lib/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
}
Expand Down
9 changes: 7 additions & 2 deletions lib/prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down

0 comments on commit 4e62f87

Please sign in to comment.