Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add emptyLangClass option #343

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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