您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

5 年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. gitbook.require(["jQuery"], function(jQuery) {
  2. /*
  3. * jQuery Highlight plugin
  4. *
  5. * Based on highlight v3 by Johann Burkard
  6. * http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
  7. *
  8. * Code a little bit refactored and cleaned (in my humble opinion).
  9. * Most important changes:
  10. * - has an option to highlight only entire words (wordsOnly - false by default),
  11. * - has an option to be case sensitive (caseSensitive - false by default)
  12. * - highlight element tag and class names can be specified in options
  13. *
  14. * Copyright (c) 2009 Bartek Szopka
  15. *
  16. * Licensed under MIT license.
  17. *
  18. */
  19. jQuery.extend({
  20. highlight: function (node, re, nodeName, className) {
  21. if (node.nodeType === 3) {
  22. var match = node.data.match(re);
  23. if (match) {
  24. var highlight = document.createElement(nodeName || 'span');
  25. highlight.className = className || 'highlight';
  26. var wordNode = node.splitText(match.index);
  27. wordNode.splitText(match[0].length);
  28. var wordClone = wordNode.cloneNode(true);
  29. highlight.appendChild(wordClone);
  30. wordNode.parentNode.replaceChild(highlight, wordNode);
  31. return 1; //skip added node in parent
  32. }
  33. } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
  34. !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
  35. !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
  36. for (var i = 0; i < node.childNodes.length; i++) {
  37. i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
  38. }
  39. }
  40. return 0;
  41. }
  42. });
  43. jQuery.fn.unhighlight = function (options) {
  44. var settings = { className: 'highlight', element: 'span' };
  45. jQuery.extend(settings, options);
  46. return this.find(settings.element + "." + settings.className).each(function () {
  47. var parent = this.parentNode;
  48. parent.replaceChild(this.firstChild, this);
  49. parent.normalize();
  50. }).end();
  51. };
  52. jQuery.fn.highlight = function (words, options) {
  53. var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
  54. jQuery.extend(settings, options);
  55. if (words.constructor === String) {
  56. words = [words];
  57. // also match 'foo-bar' if search for 'foo bar'
  58. if (/\s/.test(words[0])) words.push(words[0].replace(/\s+/, '-'));
  59. }
  60. words = jQuery.grep(words, function(word, i){
  61. return word !== '';
  62. });
  63. words = jQuery.map(words, function(word, i) {
  64. return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  65. });
  66. if (words.length === 0) { return this; }
  67. var flag = settings.caseSensitive ? "" : "i";
  68. var pattern = "(" + words.join("|") + ")";
  69. if (settings.wordsOnly) {
  70. pattern = "\\b" + pattern + "\\b";
  71. }
  72. var re = new RegExp(pattern, flag);
  73. return this.each(function () {
  74. jQuery.highlight(this, re, settings.element, settings.className);
  75. });
  76. };
  77. });