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: expose full info string #190

Merged
merged 2 commits into from
Dec 15, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import hljs from 'highlight.js';
const marked = new Marked(
markedHighlight({
langPrefix: 'hljs language-',
highlight(code, lang) {
highlight(code, lang, info) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
return hljs.highlight(code, { language }).value;
}
Expand Down Expand Up @@ -46,7 +46,7 @@ import pygmentize from 'pygmentize-bundled';
const marked = new Marked(
markedHighlight({
async: true,
highlight(code, lang) {
highlight(code, lang, info) {
return new Promise((resolve, reject) => {
pygmentize({ lang, format: 'html' }, code, function (err, result) {
if (err) {
Expand Down
67 changes: 67 additions & 0 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,71 @@ no need to escape chars

expect(() => marked(markdown)).toThrow(/set the async option to true/i);
});

const markdownWithSpaceInLang = `
\`\`\`ts twoslash
let a = 1
\`\`\``;

test('uses infostring', () => {
marked.use(markedHighlight({
highlight(code, lang, info) {
return info;
}
}));

expect(marked(markdownWithSpaceInLang)).toMatchInlineSnapshot(`
"<pre><code class="language-ts">ts twoslash
</code></pre>"
`);
});

test('async uses infostring', async() => {
marked.use(markedHighlight({
async: true,
highlight(code, lang, info) {
return new Promise((resolve, reject) => {
resolve(info);
});
}
}));

expect(await marked(markdownWithSpaceInLang)).toMatchInlineSnapshot(`
"<pre><code class="language-ts">ts twoslash
</code></pre>"
`);
});

const markdownWithoutLang = `
\`\`\`
no language provided
\`\`\`
`;

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

test('async nullish infostring is cast to empty string', async() => {
marked.use(markedHighlight({
async: true,
highlight(code, lang, info) {
expect(info).toBe('');
return Promise.resolve(info);
}
}));
expect(await marked(markdownWithoutLang)).toMatchInlineSnapshot(`
"<pre><code>
</code></pre>"
`);
});
});
8 changes: 6 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ declare module 'marked-highlight' {
* @param code The raw code to be highlighted
* @param language The language tag found immediately after the code block
* opening marker (e.g. ```typescript -> language='typescript')
* @param info The full string after the code block opening marker
* (e.g. ```ts twoslash -> info='ts twoslash')
* @return The highlighted code as a HTML string
*/
type SyncHighlightFunction = (code: string, language: string) => string;
type SyncHighlightFunction = (code: string, language: string, info: string) => string;

/**
* An asynchronous function to highlight code
*
* @param code The raw code to be highlighted
* @param language The language tag found immediately after the code block
* opening marker (e.g. ```typescript -> language='typescript')
* @param info The full string after the code block opening marker
* (e.g. ```ts twoslash -> info='ts twoslash')
* @return A Promise for the highlighted code as a HTML string
*/
type AsyncHighlightFunction = (code: string, language: string) => Promise<string>;
type AsyncHighlightFunction = (code: string, language: string, info: string) => Promise<string>;

/**
* Options for configuring the marked-highlight extension using a synchronous
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export function markedHighlight(options) {
const lang = getLang(token);

if (options.async) {
return Promise.resolve(options.highlight(token.text, lang)).then(updateToken(token));
return Promise.resolve(options.highlight(token.text, lang, token.lang || '')).then(updateToken(token));
}

const code = options.highlight(token.text, lang);
const code = options.highlight(token.text, lang, token.lang || '');
if (code instanceof Promise) {
throw new Error('markedHighlight is not set to async but the highlight function is async. Set the async option to true on markedHighlight to await the async highlight function.');
}
Expand Down
5 changes: 5 additions & 0 deletions types_test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {expectError} from 'tsd';

// Single function argument
markedHighlight((code: string, language: string) => code+language);
markedHighlight((code: string, language: string, info: string) => code+language+info);

// Invalid asynchronous function argument - missing async: true
expectError(markedHighlight(async (code: string, language: string) => code+language));
Expand Down Expand Up @@ -37,6 +38,10 @@ markedHighlight({
highlight: async (code: string, language: string) => code+language,
async: true,
})
markedHighlight({
highlight: async (code: string, language: string, info: string) => code+language+info,
async: true,
})

// Full asynchronous options argument
markedHighlight({
Expand Down