Skip to content

Commit

Permalink
Improve markdown support.
Browse files Browse the repository at this point in the history
1. Support `marked`, you can using it instead. impress#811
2. Auto trim the indentation, so it can be indent like regular HTML.
   impress#812
  • Loading branch information
thawk committed Aug 15, 2022
1 parent 78c954a commit cd5ce77
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 74 deletions.
1 change: 1 addition & 0 deletions examples/markdown/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ <h1>CSS &amp; JavaScript magic</h1>
<script type="text/javascript" src="../../extras/highlight/highlight.pack.js"></script>
<script type="text/javascript" src="../../extras/mermaid/mermaid.min.js"></script>
<script type="text/javascript" src="../../extras/markdown/markdown.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> -->

<!--
To make all described above really work, you need to include impress.js in the page.
Expand Down
121 changes: 84 additions & 37 deletions js/impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2023,48 +2023,95 @@
* Copyright 2016 Henrik Ingo (@henrikingo)
* Released under the MIT license.
*/
/* global markdown, hljs, mermaid, impress, document, window */
/* global markdown, marked, hljs, mermaid, impress */

( function( document, window ) {
"use strict";

var preInit = function() {
if ( window.markdown ) {

// Unlike the other extras, Markdown.js doesn't by default do anything in
// particular. We do it ourselves here.
// In addition, we use "-----" as a delimiter for new slide.

// Query all .markdown elements and translate to HTML
var markdownDivs = document.querySelectorAll( ".markdown" );
for ( var idx = 0; idx < markdownDivs.length; idx++ ) {
var element = markdownDivs[ idx ];
var dialect = element.dataset.markdownDialect;

var slides = element.textContent.split( /^-----$/m );
var i = slides.length - 1;
element.innerHTML = markdown.toHTML( slides[ i ], dialect );

// If there's an id, unset it for last, and all other, elements,
// and then set it for the first.
var id = null;
if ( element.id ) {
id = element.id;
element.id = "";
}
i--;
while ( i >= 0 ) {
var newElement = element.cloneNode( false );
newElement.innerHTML = markdown.toHTML( slides[ i ], dialect );
element.parentNode.insertBefore( newElement, element );
element = newElement;
i--;
}
if ( id !== null ) {
element.id = id;
}
const SLIDE_SEPARATOR = /^-----$/m;

const getMarkdownParser = function( ) {
if ( window.hasOwnProperty( "marked" ) ) {

// Using marked
return function( elem, src ) {
return marked.parse( src );
};
} else if ( window.hasOwnProperty( "markdown" ) ) {

// Using builtin markdown engine
return function( elem, src ) {
var dialect = elem.dataset.markdownDialect;
return markdown.toHTML( src, dialect );
};
}

return null;
};

const getMarkdownSlides = function( elem ) {
var text = elem.textContent;

// Using first not blank line to detect leading whitespaces.
// can't properly handle the mixing of space and tabs
var m = text.match( /^([ \t]*)\S/m );
if ( m !== null ) {
text = text.replace( new RegExp( "^" + m[ 1 ], "mg" ), "" );
}

return text.split( SLIDE_SEPARATOR );
};

const convertMarkdowns = function( selector ) {

// Detect markdown engine
var parseMarkdown = getMarkdownParser();
if ( !parseMarkdown ) {
return;
}

for ( var elem of document.querySelectorAll( selector ) ) {
var id = null;
if ( elem.id ) {
id = elem.id;
elem.id = "";
}

var origTitle = null;
if ( elem.title ) {
origTitle = elem.title;
elem.title = "";
}

var slides = getMarkdownSlides( elem );
var slideElems = [ elem ];

for ( var j = 1; j < slides.length; ++j ) {
var newElem = elem.cloneNode( false );
newElem.id = "";
elem.parentNode.insertBefore( newElem, slideElems[ 0 ] );
slideElems.splice( 0, 0, newElem );
}

if ( id ) {
slideElems[ 0 ].id = id;
}
} // Markdown

for ( var i = 0; i < slides.length; ++i ) {
slideElems[ i ].innerHTML =
parseMarkdown( slideElems[ i ], slides[ i ] );

if ( origTitle && ( i === 0 ) ) {
slideElems[ i ].title = origTitle;
}
}
}
};

var preInit = function() {

// Query all .markdown elements and translate to HTML
convertMarkdowns( ".markdown" );

if ( window.hljs ) {
hljs.initHighlightingOnLoad();
Expand Down
121 changes: 84 additions & 37 deletions src/plugins/extras/extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,95 @@
* Copyright 2016 Henrik Ingo (@henrikingo)
* Released under the MIT license.
*/
/* global markdown, hljs, mermaid, impress, document, window */
/* global markdown, marked, hljs, mermaid, impress */

( function( document, window ) {
"use strict";

var preInit = function() {
if ( window.markdown ) {

// Unlike the other extras, Markdown.js doesn't by default do anything in
// particular. We do it ourselves here.
// In addition, we use "-----" as a delimiter for new slide.

// Query all .markdown elements and translate to HTML
var markdownDivs = document.querySelectorAll( ".markdown" );
for ( var idx = 0; idx < markdownDivs.length; idx++ ) {
var element = markdownDivs[ idx ];
var dialect = element.dataset.markdownDialect;

var slides = element.textContent.split( /^-----$/m );
var i = slides.length - 1;
element.innerHTML = markdown.toHTML( slides[ i ], dialect );

// If there's an id, unset it for last, and all other, elements,
// and then set it for the first.
var id = null;
if ( element.id ) {
id = element.id;
element.id = "";
}
i--;
while ( i >= 0 ) {
var newElement = element.cloneNode( false );
newElement.innerHTML = markdown.toHTML( slides[ i ], dialect );
element.parentNode.insertBefore( newElement, element );
element = newElement;
i--;
}
if ( id !== null ) {
element.id = id;
}
const SLIDE_SEPARATOR = /^-----$/m;

const getMarkdownParser = function( ) {
if ( window.hasOwnProperty( "marked" ) ) {

// Using marked
return function( elem, src ) {
return marked.parse( src );
};
} else if ( window.hasOwnProperty( "markdown" ) ) {

// Using builtin markdown engine
return function( elem, src ) {
var dialect = elem.dataset.markdownDialect;
return markdown.toHTML( src, dialect );
};
}

return null;
};

const getMarkdownSlides = function( elem ) {
var text = elem.textContent;

// Using first not blank line to detect leading whitespaces.
// can't properly handle the mixing of space and tabs
var m = text.match( /^([ \t]*)\S/m );
if ( m !== null ) {
text = text.replace( new RegExp( "^" + m[ 1 ], "mg" ), "" );
}

return text.split( SLIDE_SEPARATOR );
};

const convertMarkdowns = function( selector ) {

// Detect markdown engine
var parseMarkdown = getMarkdownParser();
if ( !parseMarkdown ) {
return;
}

for ( var elem of document.querySelectorAll( selector ) ) {
var id = null;
if ( elem.id ) {
id = elem.id;
elem.id = "";
}
} // Markdown

var origTitle = null;
if ( elem.title ) {
origTitle = elem.title;
elem.title = "";
}

var slides = getMarkdownSlides( elem );
var slideElems = [ elem ];

for ( var j = 1; j < slides.length; ++j ) {
var newElem = elem.cloneNode( false );
newElem.id = "";
elem.parentNode.insertBefore( newElem, slideElems[ 0 ] );
slideElems.splice( 0, 0, newElem );
}

if ( id ) {
slideElems[ 0 ].id = id;
}

for ( var i = 0; i < slides.length; ++i ) {
slideElems[ i ].innerHTML =
parseMarkdown( slideElems[ i ], slides[ i ] );

if ( origTitle && ( i === 0 ) ) {
slideElems[ i ].title = origTitle;
}
}
}
};

var preInit = function() {

// Query all .markdown elements and translate to HTML
convertMarkdowns( ".markdown" );

if ( window.hljs ) {
hljs.initHighlightingOnLoad();
Expand Down

0 comments on commit cd5ce77

Please sign in to comment.