Skip to content

Commit

Permalink
update highlightCodeBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
desandro committed Jul 8, 2023
1 parent fec481b commit abe7b41
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions tasks/utils/highlight-code-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ let reFirstLine = /.*\n/;

function replaceCodeBlock( match, leadingWhiteSpace, block ) {
let langMatch = block.match( reFirstLine );
let language = langMatch && langMatch[0];
// remove first line
block = block.replace( reFirstLine, '' );
if ( language ) {
language = language.trim();
}
let language = langMatch && langMatch[0] || '';
// remove leading whitespace from code block
if ( leadingWhiteSpace && leadingWhiteSpace.length ) {
let reLeadingWhiteSpace = new RegExp( '^' + leadingWhiteSpace, 'gim' );
block = block.replace( reLeadingWhiteSpace, '' );
}
// remove first line
block = block.replace( reFirstLine, '' );

// highlight code
let highlighted = language ? highlightjs.highlight( language, block, true ).value :
block;
let highlighted = block;
if ( language ) {
highlighted = highlightjs.highlight( block, {
language: language.trim(),
illegal: false,
} ).value;
}
// wrap in <pre><code>
let html = `<pre>
<code class=${language || ''}>${highlighted}</code>
</pre>`;
let html = `<pre><code class="${language}">${highlighted}</code></pre>`;
return html;
}

Expand Down

0 comments on commit abe7b41

Please sign in to comment.