Highlight.js和Showdown.js不能一起工作

时间:2017-01-09 05:54:07

标签: javascript jquery highlight highlight.js showdown

我正在尝试在启用预览时使用highlight.js

无法使用showdown.js

让highlightjs与我的预览一起使用
  

问题:如何让highlight.js与showdown.js一起使用

Codepen Demo 注意所有.js文件和css文件都在codepen设置中加载

我尝试过使用

$(document).ready(function() {
  $('pre code').each(function(i, block) {
    hljs.highlightBlock(block);
  });
});

$("#message").on('keyup paste cut', function() {
  var text = document.getElementById('message').value,
  target = document.getElementById('showdown'),
  converter = new showdown.Converter({
  parseImgDimensions: true
}),
html = converter.makeHtml(text);

target.innerHTML = html;
});

完整脚本

$(document).ready(function() {
  $('pre code').each(function(i, block) {
    hljs.highlightBlock(block);
  });
})

$('#button-link').on('click', function() {
  $('#myLink').modal('show');
});

$('#button-image').on('click', function() {
  $('#myImage').modal('show');
});

$('#button-smile').on('click', function() {
  $('#mySmile').modal('show');
});

$('#myLink').on('shown.bs.modal', function() {
  var textarea = document.getElementById("message");
  var len = textarea.value.length;
  var start = textarea.selectionStart;
  var end = textarea.selectionEnd;
  var selectedText = textarea.value.substring(start, end);
  $('#link_title').val(selectedText);
  $('#link_url').val('http://');
});

$('#myImage').on('shown.bs.modal', function() {
  $("#image_url").attr("placeholder", "http://www.example.com/image.png");
});

$("#save-image").on('click', function(e) {
  var textarea = document.getElementById("message");
  var len = textarea.value.length;
  var start = textarea.selectionStart;
  var end = textarea.selectionEnd;
  var selectedText = textarea.value.substring(start, end);
  var counter = findAvailableNumber(textarea);

  var replace_word = '![enter image description here]' + '[' + counter + ']';

  if (counter == 1) {

    if ($('input#image_width').val().length > 0) {
      var add_link = '\n\n' + '   [' + counter + ']: ' + $('#image_url').val() + ' =' + $('input#image_width').val() + 'x' + $('input#image_height').val();
    } else {
      var add_link = '\n\n' + '   [' + counter + ']: ' + $('#image_url').val();
    }

  } else {
    var add_link = '\n' + '   [' + counter + ']: ' + $('#image_url').val();
  }

  textarea.value = textarea.value.substring(0, start) + replace_word + textarea.value.substring(end, len) + add_link;
  $("#message").trigger('change');
});

$("#save-link").on('click', function(e) {
  var textarea = document.getElementById("message");
  var len = textarea.value.length;
  var start = textarea.selectionStart;
  var end = textarea.selectionEnd;
  var selectedText = textarea.value.substring(start, end);
  var counter = findAvailableNumber(textarea);

  if ($('#link_title').val().length > 0) {
    var replace_word = '[' + $('#link_title').val() + ']' + '[' + counter + ']';
  } else {
    var replace_word = '[enter link description here]' + '[' + counter + ']';
  }

  if (counter == 1) {
    var add_link = '\n\n' + '   [' + counter + ']: ' + $('#link_url').val();
  } else {
    var add_link = '\n' + '   [' + counter + ']: ' + $('#link_url').val();
  }

  textarea.value = textarea.value.substring(0, start) + replace_word + textarea.value.substring(end, len) + add_link;
  $("#message").trigger('change');
});

// Editor Buttons
$('#bold').on('click', function(e) {
  text_wrap("message", "**", "**", 'strong text');
});

$('#italic').on('click', function(e) {
  text_wrap("message", "*", "*", 'emphasized text');
});
$('#quote').on('click', function(e) {
  text_wrap("message", "> ", "", 'Blockquote');
});

$('#code').on('click', function(e) {
  code_wrap("message", "", "", 'enter code here');
});

function text_wrap(elementID, openTag, closeTag, message) {
  var textArea = $('#' + elementID);
  var len = textArea.val().length;
  var start = textArea[0].selectionStart;
  var end = textArea[0].selectionEnd;
  var selectedText = textArea.val().substring(start, end);

  if (selectedText.length > 0) {

    replacement = openTag + selectedText + closeTag;

  } else {

    replacement = openTag + message + closeTag;
  }

  textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}

function code_wrap(elementID, openTag, closeTag, message) {
  var textArea = $('#' + elementID);
  var len = textArea.val().length;
  var start = textArea[0].selectionStart;
  var end = textArea[0].selectionEnd;
  var selectedText = textArea.val().substring(start, end);
  var multiLineReplace = '\n' + openTag;

  if (selectedText.length > 0) {

    //using regex to replace all instances of `\n` with `\n` + your indent spaces.
    replacement = '    ' + openTag + selectedText.replace(/\n/g, multiLineReplace) + closeTag;

  } else {

    replacement = '    ' + openTag + message + closeTag;
  }

  textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}

function findAvailableNumber(textarea) {
  var number = 1;

  var a = textarea.value;

  if (a.indexOf('[1]') > -1) {

    //Find lines with links

    var matches = a.match(/(^|\n)\s*\[\d+\]:/g);

    //Find corresponding numbers

    var usedNumbers = matches.map(function(match) {
      return parseInt(match.match(/\d+/)[0]);
    });

    //Find first unused number

    var number = 1;

    while (true) {

      if (usedNumbers.indexOf(number) === -1) {

        //Found unused number

        return number;
      }

      number++;
    }
  }

  return number;
}

$("#message").on('keyup paste cut', function() {
  var text = document.getElementById('message').value,
    target = document.getElementById('showdown'),
    converter = new showdown.Converter({
      parseImgDimensions: true
    }),
    html = converter.makeHtml(text);

  target.innerHTML = html;
});

$(function() {
  $('[data-toggle="tooltip"]').tooltip()
});

0 个答案:

没有答案