Skip to content

Commit

Permalink
feat: add emptyLangClass option (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Oct 14, 2024
1 parent 08b4c67 commit 6771fa6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import hljs from 'highlight.js';
// const {markedHighlight} = globalThis.markedHighlight;
const marked = new Marked(
markedHighlight({
emptyLangClass: 'hljs',
langPrefix: 'hljs language-',
highlight(code, lang, info) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
Expand Down Expand Up @@ -87,4 +88,5 @@ const highlight = "code";
|--------|--------|---------|:------------|
| async | boolean | `false` | If the highlight function returns a promise set this to `true`. Don't forget to `await` the call to `marked.parse` |
| langPrefix | string | `'language-'` | A prefix to add to the class of the `code` tag. |
| emptyLangClass | string | `''` | The class to add to the `code` tag if the language is empty. |
| highlight | function | `(code: string, lang: string) => {}` | Required. The function to transform the code to html. |
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ no language provided
expect(await marked(markdownWithoutLang)).toMatchInlineSnapshot(`
"<pre><code>
</code></pre>"
`);
});

test('nullish infostring is cast to emptyLangClass option', () => {
marked.use(markedHighlight({
emptyLangClass: 'empty',
highlight(code, lang, info) {
expect(info).toBe('');
return info;
},
}));
expect(marked(markdownWithoutLang)).toMatchInlineSnapshot(`
"<pre><code class="empty">
</code></pre>"
`);
});

test('no class when emptyLangClass is empty string', () => {
marked.use(markedHighlight({
emptyLangClass: '',
highlight(code, lang, info) {
expect(info).toBe('');
return info;
},
}));
expect(marked(markdownWithoutLang)).toMatchInlineSnapshot(`
"<pre><code>
</code></pre>"
`);
});
});
10 changes: 10 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ declare module 'marked-highlight' {
* appended to this to form the class attribute added to the <code> element.
*/
langPrefix?: string;
/**
* The class attribute added to the <code> element if the language tag is
* empty.
*/
emptyLangClass?: string;
}

/**
Expand All @@ -61,6 +66,11 @@ declare module 'marked-highlight' {
* appended to this to form the class attribute added to the <code> element.
*/
langPrefix?: string;
/**
* The class attribute added to the <code> element if the language tag is
* empty.
*/
emptyLangClass?: string;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function markedHighlight(options) {
options.langPrefix = 'language-';
}

if (typeof options.emptyLangClass !== 'string') {
options.emptyLangClass = '';
}

return {
async: !!options.async,
walkTokens(token) {
Expand Down Expand Up @@ -42,8 +46,9 @@ export function markedHighlight(options) {
code = code.text;
}
const lang = getLang(infoString);
const classAttr = lang
? ` class="${options.langPrefix}${escape(lang)}"`
const classValue = lang ? options.langPrefix + escape(lang) : options.emptyLangClass;
const classAttr = classValue
? ` class="${classValue}"`
: '';
code = code.replace(/\n$/, '');
return `<pre><code${classAttr}>${escaped ? code : escape(code, true)}\n</code></pre>`;
Expand Down

0 comments on commit 6771fa6

Please sign in to comment.