@@ -212,8 +212,70 @@ function postProcess(code) {
212212 result . find ( 'a:not([target])' ) . attr ( 'target' , '_blank' ) ;
213213 return result ;
214214}
215+
216+ //extract markdown body to html and compile to template
217+ function exportToHTML ( view ) {
218+ var title = renderTitle ( ui . area . markdown ) ;
219+ var filename = renderFilename ( ui . area . markdown ) + '.html' ;
220+ var src = view . clone ( ) ;
221+ var eles = src . find ( '*' ) ;
222+ //remove syncscroll parts
223+ eles . removeClass ( 'part' ) ;
224+ src . find ( '*[class=""]' ) . removeAttr ( 'class' ) ;
225+ eles . removeAttr ( 'data-startline data-endline' ) ;
226+ eles . find ( "a[href^='#'][smoothhashscroll]" ) . removeAttr ( 'smoothhashscroll' ) ;
227+ //remove gist content
228+ src . find ( "code[data-gist-id]" ) . children ( ) . remove ( ) ;
229+ //disable todo list
230+ src . find ( "input.task-list-item-checkbox" ) . attr ( 'disabled' , '' ) ;
231+ //replace emoji image path
232+ src . find ( "img.emoji" ) . each ( function ( key , value ) {
233+ var name = $ ( value ) . attr ( 'alt' ) ;
234+ name = name . substr ( 1 ) ;
235+ name = name . slice ( 0 , name . length - 1 ) ;
236+ $ ( value ) . attr ( 'src' , 'https://www.tortue.me/emoji/' + name + '.png' ) ;
237+ } ) ;
238+ //replace video to iframe
239+ src . find ( "div[videoid]" ) . each ( function ( key , value ) {
240+ var id = $ ( value ) . attr ( 'videoid' ) ;
241+ var style = $ ( value ) . attr ( 'style' ) ;
242+ var url = null ;
243+ if ( $ ( value ) . hasClass ( 'youtube' ) ) {
244+ url = 'https://www.youtube.com/embed/' ;
245+ } else if ( $ ( value ) . hasClass ( 'vimeo' ) ) {
246+ url = 'https://player.vimeo.com/video/' ;
215247 }
248+ if ( url ) {
249+ var iframe = $ ( '<iframe frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>' ) ;
250+ iframe . attr ( 'src' , url + id ) ;
251+ iframe . attr ( 'style' , style ) ;
252+ $ ( value ) . html ( iframe ) ;
216253 }
254+ } ) ;
255+ //generate toc
256+ var toc = $ ( '#toc' ) . clone ( ) ;
257+ toc . find ( '*' ) . removeClass ( 'active' ) ;
258+ var tocAffix = $ ( '#toc-affix' ) . clone ( ) ;
259+ tocAffix . find ( '*' ) . removeClass ( 'active' ) ;
260+ //generate html via template
261+ $ . get ( '/css/html.min.css' , function ( css ) {
262+ $ . get ( '/views/html.hbs' , function ( data ) {
263+ var template = Handlebars . compile ( data ) ;
264+ var context = {
265+ title : title ,
266+ css : css ,
267+ html : src [ 0 ] . outerHTML ,
268+ toc : toc . html ( ) ,
269+ 'toc-affix' : tocAffix . html ( )
270+ } ;
271+ var html = template ( context ) ;
272+ // console.log(html);
273+ var blob = new Blob ( [ html ] , {
274+ type : "text/html;charset=utf-8"
275+ } ) ;
276+ saveAs ( blob , filename ) ;
277+ } ) ;
278+ } ) ;
217279}
218280
219281//jQuery sortByDepth
0 commit comments