问题分析
1. 标题的锚点会被替换
中文替换为''
空格等非正常字符替换为'-'
这给自己手动写toc也带来了麻烦,不能直接复制标题作为链接
2. 不能自动生成TOC
目前gitlab不支持TOC功能
解决方法
1. 解决中文问题
直接找到源码 `
embedded/service/gitlab-rails/lib/redcarpet/render/gitlab_html.rb
`
修改生成id的部分,注意规则
47 def header(text, level)
48 if @options[:no_header_anchors]
49 "<h#{level}>#{text}</h#{level}>"
50 else
51 #id = ActionController::Base.helpers.strip_tags(h.gfm(text)).downcase() \
52 # .gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-/, '').gsub(/-$/, '')
53 # edit by renfeng, 让锚点支持中文
54 id = ActionController::Base.helpers.strip_tags(h.gfm(text)).downcase() \
55 .gsub(/[\s]/, '-')
56 "<h#{level} id=\"#{id}\">#{text}<a href=\"\##{id}\"></a></h#{level}>"
57 end
58 end
2. 自动生成toc功能
`
embedded/service/gitlab-rails/app/helpers/gitlab_markdown_helper.rb
`
markdown函数前面 查找[TOC] 生成TOC, 函数结尾把toc加到最前面.
此时的限制: 只能在文档第一行加上[TOC] 否则不认识, 可以自行优化
31 def markdown(text, options={})
32 unless (@markdown and options == @options)
33 # eidt by renfeng.mei, support [TOC]
34 toc = nil
35 if text != nil && !text.empty? && text.lines.first.match("\\[TOC\\]") != nil
36 html_toc = Redcarpet::Markdown.new(Redcarpet::Render::HTML_TOC, space_after_headers: true)
37 toc = html_toc.render(text)
38
39 text["[TOC]"]= ""
40 end
41 @options = options
42 gitlab_renderer = Redcarpet::Render::GitlabHTML.new(self, {
43 # see https://github.com/vmg/redcarpet#darling-i-packed-you-a-couple-renderers-fo r-lunch-
44 filter_html: true,
45 with_toc_data: true,
46 safe_links_only: true
47 }.merge(options))
48 @markdown = Redcarpet::Markdown.new(gitlab_renderer,
49 # see https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
50 no_intra_emphasis: true,
51 tables: true,
52 fenced_code_blocks: true,
53 autolink: true,
54 strikethrough: true,
55 lax_spacing: true,
56 space_after_headers: true,
57 superscript: true)
58 end
59 # eidt by renfeng.mei
60 if toc == nil
61 @markdown.render(text).html_safe
62 else
63 (toc + @markdown.render(text)).html_safe
64 end
65 #@markdown.render(text).html_safe
66 end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)