Last active
July 11, 2018 08:02
-
-
Save jemsgit/20acd097acb32f3b4a08fdaa3af75ce5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" | |
document.head.appendChild(script); | |
var imageCounter = 0; | |
function getLinkFromHeader(item){ | |
var a = $(item).find('a')[0]; | |
if(a && a.href){ | |
return a.href; | |
} | |
} | |
var handlers = { | |
H1: function(item){ | |
var text = item.textContent; | |
var link = getLinkFromHeader(item); | |
if(link){ | |
return '# ' + '[' + text + ']' + '(' + link + ')'; | |
} | |
return '# ' + text; | |
}, | |
H3: function(item){ | |
var text = item.textContent; | |
var link = getLinkFromHeader(item); | |
if(link){ | |
return '## ' + '[' + text + ']' + '(' + link + ')'; | |
} | |
return '## ' + text; | |
}, | |
H4: function(item){ | |
var text = item.textContent; | |
var link = getLinkFromHeader(item); | |
if(link){ | |
return '### ' + '[' + text + ']' + '(' + link + ')'; | |
} | |
return '### ' + text; | |
}, | |
IMG: function(item){ | |
console.log(item.src); | |
var link = item.src; | |
imageCounter++; | |
return ''; | |
}, | |
A: function(item){ | |
var link = item.href, | |
text = item.textContent; | |
return '[' + text + '](' + link + ')'; | |
}, | |
IFRAME: function(item){ | |
var scripts = $($(item).contents()[0]).find('script'); | |
if(scripts && scripts.length && scripts[0]){ | |
var link = scripts[0].src; | |
if(link.indexOf('git')){ | |
return '<script src=' + link +'></script>' | |
} return undefined; | |
} return undefined; | |
}, | |
PRE: function(item){ | |
return '```' + item.innerText + '```'; | |
}, | |
P: function(item){ | |
return '\r\n' + item.innerText + '\r\n'; | |
}, | |
BLOCKQUOTE: function(item){ | |
return '\r\n' + item.innerText + '\r\n'; | |
}, | |
LI: function(item){ | |
return '\r\n * ' + item.innerText + '\r\n'; | |
} | |
} | |
var items = $('p, blockquote, .graf.graf--h3, .graf.graf--h4, img.progressiveMedia-image.js-progressiveMedia-image, a.markup--anchor.markup--p-anchor, li.graf--li, div.iframeContainer iframe, .graf.graf--pre.graf-after--p'); | |
window.resultItems = []; | |
$.each(items, function(index, item){ | |
var processFunc = handlers[item.tagName.toUpperCase()]; | |
if(processFunc){ | |
var result = processFunc(item); | |
window.resultItems.push(result); | |
} | |
}) | |
console.log(window.resultItems.join('\r\n\r\n')); | |
})() | |
(function(){ | |
var shift = 0; | |
var handlers = { | |
H1: function(item, prev){ | |
var text = item.textContent; | |
return prev + '\r\n' + '# ' + text; | |
}, | |
H3: function(item, prev){ | |
var text = item.textContent; | |
return prev + '\r\n' + '## ' + text; | |
}, | |
H4: function(item, prev){ | |
var text = item.textContent; | |
return prev + '\r\n' + '### ' + text; | |
}, | |
PRE: function(item, prev){ | |
return prev + '\r\n' + '```' + item.innerText + '```'; | |
}, | |
P: function(item, prev){ | |
console.log(prev); | |
return prev + '\r\n' + item.innerText + '\r\n'; | |
}, | |
BLOCKQUOTE: function(item, prev){ | |
return prev + '\r\n' + item.innerText + '\r\n'; | |
}, | |
LI: function(item, prev){ | |
return prev + '\r\n * ' + item.innerText + '\r\n'; | |
} | |
} | |
var items = $('p, blockquote, .graf.graf--h3, .graf.graf--h4, img.progressiveMedia-image.js-progressiveMedia-image, a.markup--anchor.markup--p-anchor, li.graf--li, div.iframeContainer iframe, .graf.graf--pre.graf-after--p'); | |
$.each(items, function(index, item){ | |
var tagName = item.tagName.toUpperCase(); | |
var processFunc = handlers[tagName]; | |
if(tagName === 'A'){ //wrong translation of A element (duplicate) | |
var link = item.href; | |
if(window.resultItems[index-shift-1].indexOf(link) >-1) { | |
shift+= 1; | |
} | |
} | |
if(processFunc){ | |
window.resultItems[index - shift] = processFunc(item, window.resultItems[index - shift]); | |
} | |
}) | |
console.log(window.resultItems.join('\r\n\r\n')); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment