Commit cc13a158 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '346042-add-taskable-capture-group' into 'master'

Add capture group to Taskable::ITEM_PATTERN

See merge request gitlab-org/gitlab!80313
parents 6b6df866 5996af64
......@@ -15,17 +15,16 @@ module Taskable
INCOMPLETE_PATTERN = /(\[\s\])/.freeze
ITEM_PATTERN = %r{
^
(?:(?:>\s{0,4})*) # optional blockquote characters
(?:\s*(?:[-+*]|(?:\d+\.)))+ # list prefix (one or more) required - task item has to be always in a list
\s+ # whitespace prefix has to be always presented for a list item
(\[\s\]|\[[xX]\]) # checkbox
(\s.+) # followed by whitespace and some text.
(?:(?:>\s{0,4})*) # optional blockquote characters
((?:\s*(?:[-+*]|(?:\d+\.)))+) # list prefix (one or more) required - task item has to be always in a list
\s+ # whitespace prefix has to be always presented for a list item
(\[\s\]|\[[xX]\]) # checkbox
(\s.+) # followed by whitespace and some text.
}x.freeze
def self.get_tasks(content)
content.to_s.scan(ITEM_PATTERN).map do |checkbox, label|
# ITEM_PATTERN strips out the hyphen, but Item requires it. Rabble rabble.
TaskList::Item.new("- #{checkbox}", label.strip)
content.to_s.scan(ITEM_PATTERN).map do |prefix, checkbox, label|
TaskList::Item.new("#{prefix} #{checkbox}", label.strip)
end
end
......
......@@ -38,7 +38,7 @@ class TaskListToggleService
return unless markdown_task.chomp == line_source
return unless source_checkbox = Taskable::ITEM_PATTERN.match(markdown_task)
currently_checked = TaskList::Item.new(source_checkbox[1]).complete?
currently_checked = TaskList::Item.new(source_checkbox[2]).complete?
# Check `toggle_as_checked` to make sure we don't accidentally replace
# any `[ ]` or `[x]` in the middle of the text
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Taskable do
using RSpec::Parameterized::TableSyntax
describe '.get_tasks' do
let(:description) do
<<~MARKDOWN
Any text before the list
- [ ] First item
- [x] Second item
* [x] First item
* [ ] Second item
MARKDOWN
end
let(:expected_result) do
[
TaskList::Item.new('- [ ]', 'First item'),
TaskList::Item.new('- [x]', 'Second item'),
TaskList::Item.new('* [x]', 'First item'),
TaskList::Item.new('* [ ]', 'Second item')
]
end
subject { described_class.get_tasks(description) }
it { is_expected.to match(expected_result) }
end
describe '#task_list_items' do
where(issuable_type: [:issue, :merge_request])
with_them do
let(:issuable) { build(issuable_type, description: description) }
subject(:result) { issuable.task_list_items }
context 'when description is present' do
let(:description) { 'markdown' }
it 'gets tasks from markdown' do
expect(described_class).to receive(:get_tasks)
result
end
end
context 'when description is blank' do
let(:description) { '' }
it 'returns empty array' do
expect(result).to be_empty
end
it 'does not try to get tasks from markdown' do
expect(described_class).not_to receive(:get_tasks)
result
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment