@@ -4,6 +4,8 @@ import PrismLoader from "./src/PrismLoader.js";
44import hasTemplateFormat from "./src/hasTemplateFormat.js" ;
55import HighlightPairedShortcode from "./src/HighlightPairedShortcode.js" ;
66
7+ const TRIPLE_BACKTICK = "```" ;
8+
79export default function ( eleventyConfig , options ) {
810 try {
911 eleventyConfig . versionCheck ( pkg [ "11ty" ] . compatibility ) ;
@@ -13,7 +15,6 @@ export default function(eleventyConfig, options){
1315 options = Object . assign ( {
1416 init : function ( { Prism} ) { } ,
1517 lineSeparator : "\n" ,
16- lineHighlightSeparator : " " ,
1718 errorOnInvalidLanguage : false ,
1819 alwaysWrapLineHighlights : false ,
1920 preAttributes : { } ,
@@ -25,20 +26,27 @@ export default function(eleventyConfig, options){
2526 PrismLoader ( language )
2627 }
2728
29+ function shortcode ( content , args ) {
30+ // {% highlight "js 0 2-3" %}
31+ let [ language , ...highlightNumbers ] = ( args || "" ) . split ( " " ) ;
32+
33+ // Issue #77 avoid issues when nesting code blocks inside other elements (e.g. <div>{% highlight %}…</div>), introducing extra paragraphs
34+ if ( this . page . inputPath . endsWith ( ".md" ) ) {
35+ let highlightNumbersStr = highlightNumbers . length > 0 ? `/${ highlightNumbers . join ( "/" ) } ` : "" ;
36+ return `\n\n${ TRIPLE_BACKTICK } ${ language || "" } ${ highlightNumbersStr }
37+ ${ content }
38+ ${ TRIPLE_BACKTICK } \n\n`;
39+ }
40+
41+ return HighlightPairedShortcode ( content , language , highlightNumbers . join ( " " ) , options ) ;
42+ }
43+
2844 if ( hasTemplateFormat ( options . templateFormats , "liquid" ) ) {
29- eleventyConfig . addPairedLiquidShortcode ( "highlight" , function ( content , args ) {
30- // {% highlight "js 0 2-3" %}
31- let [ language , ...highlightNumbers ] = ( args || "" ) . split ( " " ) ;
32- return HighlightPairedShortcode ( content , language , highlightNumbers . join ( " " ) , options ) ;
33- } ) ;
45+ eleventyConfig . addPairedLiquidShortcode ( "highlight" , shortcode ) ;
3446 }
3547
3648 if ( hasTemplateFormat ( options . templateFormats , "njk" ) ) {
37- eleventyConfig . addPairedNunjucksShortcode ( "highlight" , function ( content , args ) {
38- // {% highlight "js 0 2-3" %}
39- let [ language , ...highlightNumbers ] = ( args || "" ) . split ( " " ) ;
40- return HighlightPairedShortcode ( content , language , highlightNumbers . join ( " " ) , options ) ;
41- } ) ;
49+ eleventyConfig . addPairedNunjucksShortcode ( "highlight" , shortcode ) ;
4250 }
4351
4452 if ( hasTemplateFormat ( options . templateFormats , "md" ) ) {
0 commit comments