当前位置:   article > 正文

stackedit_在StackEdit中实现TitleCapitalization

title captalization

stackedit

在SitePoint上为PHP Channel撰写帖子时,我经常忘记如何正确使用标题。 我通常最终会使用TitleCapitalization进行快速修复,但是我常常幻想在StackEdit的title字段旁边有一个按钮来快速自动应用。 好了,我们已经介绍了启动和运行本地(或多个)StackEdit的问题。 为什么也不要构建按钮?

03

准备好

为了准备升级,我们需要签出StackEdit的本地副本。 我当然会使用我可信赖的旧版宅基地改良盒,就像这里一样。 您可以使用自己的Linux操作系统,具体取决于您,但这绝对是最简单的。

  1. git clone https://github.com/swader/homestead-improved hi_stackedit
  2. cd hi_stackedit
  3. vagrant up
  4. vagrant ssh

进入虚拟机后,我们将克隆StackEdit。

  1. cd ~/Code
  2. git clone https://github.com/benweet/stackedit
  3. cd stackedit
  4. npm install --no-bin-link

请注意,如果在运行此命令时遇到“无法解决”错误,并且以典型的节点“冗长”的方式出现了许多其他错误,则仅意味着某些依赖项丢失了。 进入package.json并从第23 第28 删除主题标签值(如果它们仍然存在)。 这些标记引用的版本不再存在,并且在撰写本文时,StackEdit的作者仍未更新StackEdit的文件以反映这一点。

bower install

这需要一段时间。 如果安装了BowerPHP ,则可以使用它。

要运行本地副本,请执行以下操作:

(export PORT=5000 && node server.js)

然后,在浏览器中访问homestead.app:5000 (或您设置的任何主机,如果不是默认的homestead.app )。

实作

好吧,让我们开始吧。 该实现将由两部分组成-UI和逻辑。

用户界面

让我们添加按钮。

StackEdit在UI方面有些复杂,难以扩展。 安装后,项目本身包含超过30000个文件,其中包含下载的依赖项和批次。 这对于Web应用程序来说是荒谬的,并且对于任何IDE都很难索引,尤其是因为JavaScript有点混乱。 向界面添加按钮需要几个步骤。 我们要寻找的外观是这样的:

02

文档标题旁边的“复选标记”图标,形式为Glyphicon ,根据使用的主题与其他UI匹配。 我使用复选标记是因为Glyphicons已包含在StackEdit的Bootstrap中。 在上下文上可能并不完美,但这是在不编辑太多文件的情况下获得所需内容的最快方法(默认情况下,我们将编辑很多文件,而增加这些开销毫无意义)。

我们需要编辑的视图是public/res/html/bodyEditor.html –我们将在第44行周围添加一个新的图标容器:

  1. <li><div class="working-indicator"></div></li>
  2. <li><div class="capitalize-button"></div></li>
  3. <li><a class="btn btn-success file-title-navbar" href="#" title="Rename document"> </a></li>

我们在“工作指示器”容器之后添加了一个“大写按钮”容器,因此我们的按钮出现在标题旁边,与标题的上下文最匹配。 不过,这只是容器。

StackEdit UI中的所有按钮都是使用JS构建的。 这发生在文件public/res/libs/Markdown.Editor.js 。 首先,让我们添加按钮标签。 该文件的顶部是defaultStrings数组。 编辑它以包含我们的标题大写标签,如下所示:

  1. [...]
  2. help: "Markdown Editing Help",
  3. titlecapitalization: "Autocapitalize Title"
  4. };

然后,向下滚动到同一文件中的makeSpritedButtonRow函数,并在if (helpOptions) {块上方添加以下内容:

  1. buttons.titlecapitalization = makeButton("wmd-titlecapitalization", getString("titlecapitalization"), "-240px", bindCommand(function (chunk, postProcessing) {
  2. alert("Hello");
  3. }));

这将创建一个与其余编辑器主题匹配的按钮,并为它提供一个title属性以及我们定义的字符串,因此当我们将鼠标悬停在该按钮上时可以看到它。 单击时还将显示“ Hello”。 但是,它仍然不会显示在界面中。 为此,我们需要编辑public/res/core.js

找到注释// Add customized buttons在该文件中// Add customized buttons ,然后转到该块的末尾。 在此添加以下内容:

$("#wmd-titlecapitalization").append($('<i class="icon-check">')).prependTo($('.capitalize-button'));

免费学习PHP!

全面介绍PHP和MySQL,从而实现服务器端编程的飞跃。

原价$ 11.95 您的完全免费

这将找到我们的按钮容器,并将我们新创建的按钮插入其中。 如果现在以调试模式( homestead.app:5000/editor?debug )刷新编辑器并单击按钮,则应该看到“ Hello”警报,如Markdown.Editor.js的回调所定义。

逻辑

现在已经添加了按钮,让我们按我们想要的去做。

首先,让我们获取标题字段的文本。 编辑Markdown.Editor.js 。 替换alert("Hello"); 在按钮的回调中包含以下内容:

console.log($(".title-container a").text());

现在单击按钮将在控制台中生成当前文档标题。 到目前为止,一切都很好。

为了获得我们想要做什么的逻辑,我们将“借用” TitleCapitalization.com中的代码。 如果您查看源代码,您会发现它位于底部的脚本标记中。 稍微清理一下以删除特定于站点的内容,最终得到以下结果:

  1. (function(){
  2. var prepositions = [
  3. 'a',
  4. 'abaft',
  5. 'aboard',
  6. 'about',
  7. 'above',
  8. 'absent',
  9. 'across',
  10. 'afore',
  11. 'after',
  12. 'against',
  13. 'along',
  14. 'alongside',
  15. 'amid',
  16. 'amidst',
  17. 'among',
  18. 'amongst',
  19. 'an',
  20. 'apropos',
  21. 'apud',
  22. 'around',
  23. 'as',
  24. 'aside',
  25. 'astride',
  26. 'at',
  27. 'athwart',
  28. 'atop',
  29. 'barring',
  30. 'before',
  31. 'behind',
  32. 'below',
  33. 'beneath',
  34. 'beside',
  35. 'besides',
  36. 'between',
  37. 'beyond',
  38. 'but',
  39. 'by',
  40. 'circa',
  41. 'concerning',
  42. 'despite',
  43. 'down',
  44. 'during',
  45. 'except',
  46. 'excluding',
  47. 'failing',
  48. 'following',
  49. 'for',
  50. 'from',
  51. 'given',
  52. 'in',
  53. 'including',
  54. 'inside',
  55. 'into',
  56. 'lest',
  57. 'like',
  58. 'mid',
  59. 'midst',
  60. 'minus',
  61. 'modulo',
  62. 'near',
  63. 'next',
  64. 'notwithstanding',
  65. 'of',
  66. 'off',
  67. 'on',
  68. 'onto',
  69. 'opposite',
  70. 'out',
  71. 'outside',
  72. 'over',
  73. 'pace',
  74. 'past',
  75. 'per',
  76. 'plus',
  77. 'pro',
  78. 'qua',
  79. 'regarding',
  80. 'round',
  81. 'sans',
  82. // while it technically can be a preoposition,
  83. // (http://www.merriam-webster.com/thesaurus/save[preposition])
  84. // it is usually used as a verb
  85. // 'save',
  86. 'since',
  87. 'than',
  88. 'through',
  89. 'thru',
  90. 'throughout',
  91. 'thruout',
  92. 'till',
  93. 'times',
  94. 'to',
  95. 'toward',
  96. 'towards',
  97. 'under',
  98. 'underneath',
  99. 'unlike',
  100. 'until',
  101. 'unto',
  102. 'up',
  103. 'upon',
  104. 'versus',
  105. 'vs\.',
  106. 'vs',
  107. 'v\.',
  108. 'v',
  109. 'via',
  110. 'vice',
  111. 'with',
  112. 'within',
  113. 'without',
  114. 'worth'
  115. ];
  116. var articles = [
  117. 'a',
  118. 'an',
  119. 'the'
  120. ];
  121. var conjunctions = [
  122. 'and',
  123. 'but',
  124. 'for',
  125. 'so',
  126. 'nor',
  127. 'or',
  128. 'yet'
  129. ];
  130. // var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
  131. var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
  132. var all_lower_case = '(' + (prepositions.concat(articles).concat(conjunctions)).join('|') + ')';
  133. console.log('all lower case', all_lower_case);
  134. window.titleCaps = function(title){
  135. var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
  136. title = title.replace(/[\u2018\u2019]/g, "'")
  137. .replace(/[\u201C\u201D]/g, '"');
  138. while (true) {
  139. var m = split.exec(title);
  140. parts.push( title.substring(index, m ? m.index : title.length)
  141. .replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
  142. return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
  143. })
  144. //.replace(RegExp("\\b" + small + "\\b", "ig"), lower)
  145. //.replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
  146. // return punct + upper(word);
  147. //})
  148. //.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));
  149. .replace(RegExp("\\b" + all_lower_case + "\\b", "ig"), lower)
  150. .replace(RegExp("^" + punct + all_lower_case + "\\b", "ig"), function(all, punct, word){
  151. return punct + upper(word);
  152. })
  153. .replace(RegExp("\\b" + all_lower_case + punct + "$", "ig"), upper));
  154. index = split.lastIndex;
  155. if ( m ) parts.push( m[0] );
  156. else break;
  157. }
  158. return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
  159. .replace(/(['Õ])S\b/ig, "$1s")
  160. .replace(/\b(AT&T|Q&A)\b/ig, function(all){
  161. return all.toUpperCase();
  162. });
  163. };
  164. function lower(word){
  165. return word.toLowerCase();
  166. }
  167. function upper(word){
  168. return word.substr(0,1).toUpperCase() + word.substr(1);
  169. }
  170. })();

如果现在将其粘贴到控制台中,则可以访问名为“ titleCaps”的根函数,该根函数接受字符串并打印出标题大写的字符串。 这正是我们所需要的。

再次编辑按钮的回调,并将其更改为:

  1. var titleContainer = $('.title-container a');
  2. var capitalized = capitalize($(titleContainer).text());
  3. $(titleContainer).text(capitalized);
  4. $(".input-file-title").val(capitalized);

现在我们所缺少的只是capitalize功能。 环顾Markdown.Editor.js的代码,我们可以看到泛型函数仍然存在(例如,请参见properlyEncoded的Encoded)。 因此,我们也不需要再考虑将这样的想法纳入考虑范围。 在文件末尾,最后一个})();之前})(); ,添加以下内容:

  1. var prepositions = [
  2. 'a',
  3. 'abaft',
  4. 'aboard',
  5. 'about',
  6. 'above',
  7. 'absent',
  8. 'across',
  9. 'afore',
  10. 'after',
  11. 'against',
  12. 'along',
  13. 'alongside',
  14. 'amid',
  15. 'amidst',
  16. 'among',
  17. 'amongst',
  18. 'an',
  19. 'apropos',
  20. 'apud',
  21. 'around',
  22. 'as',
  23. 'aside',
  24. 'astride',
  25. 'at',
  26. 'athwart',
  27. 'atop',
  28. 'barring',
  29. 'before',
  30. 'behind',
  31. 'below',
  32. 'beneath',
  33. 'beside',
  34. 'besides',
  35. 'between',
  36. 'beyond',
  37. 'but',
  38. 'by',
  39. 'circa',
  40. 'concerning',
  41. 'despite',
  42. 'down',
  43. 'during',
  44. 'except',
  45. 'excluding',
  46. 'failing',
  47. 'following',
  48. 'for',
  49. 'from',
  50. 'given',
  51. 'in',
  52. 'including',
  53. 'inside',
  54. 'into',
  55. 'lest',
  56. 'like',
  57. 'mid',
  58. 'midst',
  59. 'minus',
  60. 'modulo',
  61. 'near',
  62. 'next',
  63. 'notwithstanding',
  64. 'of',
  65. 'off',
  66. 'on',
  67. 'onto',
  68. 'opposite',
  69. 'out',
  70. 'outside',
  71. 'over',
  72. 'pace',
  73. 'past',
  74. 'per',
  75. 'plus',
  76. 'pro',
  77. 'qua',
  78. 'regarding',
  79. 'round',
  80. 'sans',
  81. 'since',
  82. 'than',
  83. 'through',
  84. 'thru',
  85. 'throughout',
  86. 'thruout',
  87. 'till',
  88. 'times',
  89. 'to',
  90. 'toward',
  91. 'towards',
  92. 'under',
  93. 'underneath',
  94. 'unlike',
  95. 'until',
  96. 'unto',
  97. 'up',
  98. 'upon',
  99. 'versus',
  100. 'vs\.',
  101. 'vs',
  102. 'v\.',
  103. 'v',
  104. 'via',
  105. 'vice',
  106. 'with',
  107. 'within',
  108. 'without',
  109. 'worth'
  110. ];
  111. var articles = [
  112. 'a',
  113. 'an',
  114. 'the'
  115. ];
  116. var conjunctions = [
  117. 'and',
  118. 'but',
  119. 'for',
  120. 'so',
  121. 'nor',
  122. 'or',
  123. 'yet'
  124. ];
  125. var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
  126. var all_lower_case = '(' + (prepositions.concat(articles).concat(conjunctions)).join('|') + ')';
  127. console.log('all lower case', all_lower_case);
  128. var capitalize = function(title){
  129. var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
  130. title = title.replace(/[\u2018\u2019]/g, "'")
  131. .replace(/[\u201C\u201D]/g, '"');
  132. while (true) {
  133. var m = split.exec(title);
  134. parts.push( title.substring(index, m ? m.index : title.length)
  135. .replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
  136. return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
  137. })
  138. .replace(RegExp("\\b" + all_lower_case + "\\b", "ig"), lower)
  139. .replace(RegExp("^" + punct + all_lower_case + "\\b", "ig"), function(all, punct, word){
  140. return punct + upper(word);
  141. })
  142. .replace(RegExp("\\b" + all_lower_case + punct + "$", "ig"), upper));
  143. index = split.lastIndex;
  144. if ( m ) parts.push( m[0] );
  145. else break;
  146. }
  147. return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
  148. .replace(/(['Õ])S\b/ig, "$1s")
  149. .replace(/\b(AT&T|Q&A)\b/ig, function(all){
  150. return all.toUpperCase();
  151. });
  152. };
  153. function lower(word){
  154. return word.toLowerCase();
  155. }
  156. function upper(word){
  157. return word.substr(0,1).toUpperCase() + word.substr(1);
  158. }

如果您现在对此进行测试,您会发现“ Hello world”之类的标题被大写为“ Hello World”。 单击标题字段,您会注意到它也适用于内部文本–一切均已正确大写:

01

结论

在本文中,我们首先在本地托管了MarkDown编辑器StackEdit,从而实现了所需的新功能。 我们添加了一个按钮,从TitleCapitalization窃取了功能,并将其回收到我们的环境中。 如果愿意,我们现在可以使用此升级将拉取请求发送给项目所有者。 在您阅读本文时,它可能会被接受,也可能会被拒绝,但是无论如何,我们的本地副本已实现了功能,我们可以按预期使用它。

注释? 反馈? 让我知道!

翻译自: https://www.sitepoint.com/implementing-titlecapitalization-stackedit/

stackedit

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/197244
推荐阅读
相关标签
  

闽ICP备14008679号