Skip to content

Instantly share code, notes, and snippets.

@hecomi
Created June 5, 2021 12:35
Show Gist options
  • Save hecomi/98ae5cad921e58afb30c7c6b59ed4655 to your computer and use it in GitHub Desktop.
Save hecomi/98ae5cad921e58afb30c7c6b59ed4655 to your computer and use it in GitHub Desktop.
// Ref: http://7cc.hatenadiary.jp/entry/hatena-source-line
(function(document) {
var pres = document.getElementsByTagName('pre');
for (var i = pres.length; i--;) {
var elem = makeLineNumber(pres[i]);
pres[i].appendChild(elem);
}
function makeLineNumber(pre) {
var ol = document.createElement('ol'),
li = document.createElement('li'),
df = document.createDocumentFragment(),
br = pre.innerHTML.match(/\n/g);
if (br) {
ol.className = 'preLine';
ol.setAttribute('role', 'presentation');
for (var i = br.length; i--;) {
var cloned_li = li.cloneNode(true);
df.appendChild(cloned_li);
}
ol.appendChild(df);
}
return ol;
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment