Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created February 19, 2010 02:15
Show Gist options
  • Save cowboy/308330 to your computer and use it in GitHub Desktop.
Save cowboy/308330 to your computer and use it in GitHub Desktop.
Fix Disqus craptacular markup.
// Ben Alman
// http://benalman.com/
//
// Requires jQuery 1.3.2+ and jQuery doTimeout
// http://jquery.com/
// http://benalman.com/projects/jquery-dotimeout-plugin/
//
// SyntaxHighlighter optional (but totally awesome)
// http://alexgorbatchev.com/wiki/SyntaxHighlighter
//
// jQuery BBQ optional (IMO, don't leave home without it)
// http://benalman.com/projects/jquery-bbq-plugin/
$(function(){
// Fix Disqus craptacular markup.
$('#disqus_thread').doTimeout( 250, function(){
var that = $(this),
html = that.html(),
frag = $.param.fragment(), // You don't *need* jQuery BBQ for this.
comment_elem,
content,
data;
// Get element data store.
data = that.data( 'disqus', data = that.data( 'disqus' ) || {} );
if ( html !== data.html ) {
// The first time (only) there is any actual Disqus HTML...
if ( html !== '' && !data.has_html ) {
data.has_html = true;
// If location.hash has a Disqus #comment-12345678 reference...
if ( /^comment-\d+$/.test( frag ) ) {
// Scroll to that element, if it exists.
comment_elem = that.find( '#' + frag );
if ( comment_elem.length ) {
$('body').scrollTop( comment_elem.offset().top );
}
}
}
content = that.find( '.dsq-comment-message' );
// Unwrap any <P> text followed by a <BR>.
content.find('p + br').each(function() {
var p = $(this).prev();
p.replaceWith( p.html() );
});
// Unwrap last-child <P> text.
content.find('p:last-child').each(function() {
var p = $(this);
p.replaceWith( p.html() );
});
// Find all <PRE>
content.find('pre')
// Convert <BR> to \n
.each(function() {
var pre = $(this);
pre.html( pre.html().replace( /<br>/gi, "\n" ) );
})
// Remove any <BR> immediately following <PRE>
.next( 'br' )
.remove();
// Syntax highlighter.
SyntaxHighlighter.highlight();
data.html = html;
}
// Poll.
return true;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment