diff options
author | Vinnie Okada <vokada@mrvinn.com> | 2014-10-05 09:53:44 +0400 |
---|---|---|
committer | Vinnie Okada <vokada@mrvinn.com> | 2014-10-06 07:15:27 +0400 |
commit | 9f0083a96c03ec22b1d9442a9c7530899e633301 (patch) | |
tree | f41d0cc9e52728884085745e8cb837457a9f3ff2 /lib | |
parent | ff43500024f707a435cbcad43eb4d467368aabfe (diff) |
Add task lists to issues and merge requests
Make the Markdown parser recognize "[x]" or "[ ]" at the beginning of a
list item and turn it into a checkbox input. Users who can modify the
issue or MR can toggle the checkboxes directly or edit the Markdown to
manage the tasks. Task status is also displayed in the MR and issue
lists.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/markdown.rb | 24 | ||||
-rw-r--r-- | lib/redcarpet/render/gitlab_html.rb | 6 |
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 709a74fe21e..17512a51658 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -33,6 +33,11 @@ module Gitlab attr_reader :html_options + def gfm_with_tasks(text, project = @project, html_options = {}) + text = gfm(text, project, html_options) + parse_tasks(text) + end + # Public: Parse the provided text with GitLab-Flavored Markdown # # text - the source text @@ -265,5 +270,24 @@ module Gitlab ) link_to("#{prefix_text}##{identifier}", url, options) end + + # Turn list items that start with "[ ]" into HTML checkbox inputs. + def parse_tasks(text) + li_tag = '<li class="task-list-item">' + unchecked_box = '<input type="checkbox" value="on" disabled />' + checked_box = unchecked_box.sub(/\/>$/, 'checked="checked" />') + + # Regexp captures don't seem to work when +text+ is an + # ActiveSupport::SafeBuffer, hence the `String.new` + String.new(text).gsub(Taskable::TASK_PATTERN_HTML) do + checked = $LAST_MATCH_INFO[:checked].downcase == 'x' + + if checked + "#{li_tag}#{checked_box}" + else + "#{li_tag}#{unchecked_box}" + end + end + end end end diff --git a/lib/redcarpet/render/gitlab_html.rb b/lib/redcarpet/render/gitlab_html.rb index bb225f1acd8..c3378d6a18f 100644 --- a/lib/redcarpet/render/gitlab_html.rb +++ b/lib/redcarpet/render/gitlab_html.rb @@ -47,6 +47,10 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML unless @template.instance_variable_get("@project_wiki") || @project.nil? full_document = h.create_relative_links(full_document) end - h.gfm(full_document) + if @options[:parse_tasks] + h.gfm_with_tasks(full_document) + else + h.gfm(full_document) + end end end |