mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 09:12:45 +08:00
DEV: Remove checklist syntax migrator (#22942)
Added in https://github.com/discourse/discourse-checklist/pull/23 Those who wanted to convert most likely did in the last three years :]
This commit is contained in:
parent
b3c722f2f7
commit
7405aae85a
|
@ -1,36 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ChecklistSyntaxMigrator
|
||||
CHECKBOX_REGEX = /^( {0,3})\[(_|-|\*|\\\*)\]/
|
||||
CODE_BLOCK_REGEX = /^ {0,3}```/
|
||||
QUOTE_START_REGEX = /^ {0,3}\[quote/
|
||||
QUOTE_END_REGEX = /^ {0,3}\[\/quote\]/
|
||||
|
||||
def initialize(post)
|
||||
@post = post
|
||||
end
|
||||
|
||||
def update_syntax!
|
||||
lines = @post.raw.split("\n")
|
||||
in_code = false
|
||||
in_quote = false
|
||||
lines.each_with_index do |line, index|
|
||||
if line.match(CODE_BLOCK_REGEX)
|
||||
in_code = !in_code
|
||||
elsif line.match(QUOTE_START_REGEX)
|
||||
in_quote = true
|
||||
elsif line.match(QUOTE_END_REGEX)
|
||||
in_quote = false
|
||||
else
|
||||
next if in_code || in_quote
|
||||
|
||||
lines[index] = line.gsub(CHECKBOX_REGEX) { "#{$1}[x]" }
|
||||
end
|
||||
end
|
||||
new_raw = lines.join("\n")
|
||||
|
||||
return if new_raw == @post.raw
|
||||
@post.raw = new_raw
|
||||
@post.save!
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
desc "Convert old style checkbox markdown to new style"
|
||||
task "discourse_checklist:migrate_old_syntax" => :environment do |t|
|
||||
puts "Updating checklist syntax on all posts..."
|
||||
|
||||
Post
|
||||
.raw_match("[")
|
||||
.find_each(batch_size: 50) { |post| ChecklistSyntaxMigrator.new(post).update_syntax! }
|
||||
|
||||
puts "", "Done!"
|
||||
end
|
|
@ -9,9 +9,4 @@
|
|||
enabled_site_setting :checklist_enabled
|
||||
|
||||
register_asset "stylesheets/checklist.scss"
|
||||
|
||||
register_svg_icon "spinner"
|
||||
|
||||
after_initialize do
|
||||
["../lib/checklist_syntax_migrator.rb"].each { |path| load File.expand_path(path, __FILE__) }
|
||||
end
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "rails_helper"
|
||||
|
||||
describe ChecklistSyntaxMigrator do
|
||||
before { SiteSetting.allow_uncategorized_topics = true }
|
||||
let(:topic) { Fabricate(:topic) }
|
||||
let(:post_args) { { user: topic.user, topic: topic } }
|
||||
|
||||
def post_with_body(body)
|
||||
args = post_args.merge(raw: body)
|
||||
Fabricate.build(:post, args)
|
||||
end
|
||||
|
||||
it "replaces instances of the old checkbox instance, with the new syntax" do
|
||||
body = "[-] 1\n[_] 2\n[*] 3\n[\\*] 4"
|
||||
post = post_with_body(body)
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
|
||||
expected = "[x] 1\n[x] 2\n[x] 3\n[x] 4"
|
||||
expect(post.reload.raw).to eq(expected)
|
||||
end
|
||||
|
||||
it "does not replace if more than 3 spaces are before a checkbox" do
|
||||
body = " [\*]\n [-]"
|
||||
post = post_with_body(body)
|
||||
post.save
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
expect(post.reload.raw).to eq(body)
|
||||
end
|
||||
|
||||
it "does not replace checkboxes after text" do
|
||||
body = "what about this? [\*]"
|
||||
post = post_with_body(body)
|
||||
post.save
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
expect(post.reload.raw).to eq(body)
|
||||
end
|
||||
|
||||
it "handles each line independently" do
|
||||
body = "[-] replace that, \n this wont be changed! [\*]"
|
||||
post = post_with_body(body)
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
|
||||
expected = "[x] replace that, \n this wont be changed! [\*]"
|
||||
expect(post.reload.raw).to eq(expected)
|
||||
end
|
||||
|
||||
it "allows spaces 0,1,2 and 3 spaces before" do
|
||||
body = "[-] 0 spaces\n [-] 1 space\n [-] 2 spaces\n [-] 3 spaces\n [-] 4 spaces"
|
||||
post = post_with_body(body)
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
|
||||
expected = "[x] 0 spaces\n [x] 1 space\n [x] 2 spaces\n [x] 3 spaces\n [-] 4 spaces"
|
||||
expect(post.reload.raw).to eq(expected)
|
||||
end
|
||||
|
||||
it "does not convert checkboxes in code blocks" do
|
||||
body = [
|
||||
"```",
|
||||
"[\*] This won't be converted",
|
||||
"```",
|
||||
"[\*] That will",
|
||||
"```",
|
||||
"[\*] Again this won't",
|
||||
"```",
|
||||
].join("\n")
|
||||
post = post_with_body(body)
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
|
||||
expected = [
|
||||
"```",
|
||||
"[\*] This won't be converted",
|
||||
"```",
|
||||
"[x] That will",
|
||||
"```",
|
||||
"[\*] Again this won't",
|
||||
"```",
|
||||
].join("\n")
|
||||
expect(post.reload.raw).to eq(expected)
|
||||
end
|
||||
|
||||
it "does not convert checkboxes in block quotes" do
|
||||
body = [
|
||||
'[quote="markvanlan, post:10, topic:10"]',
|
||||
"[\*] This won't be converted",
|
||||
"[/quote]",
|
||||
"[\*] That will",
|
||||
'[quote="markvanlan, post:11, topic:10"]',
|
||||
"[\*] Again this won't",
|
||||
"[/quote]",
|
||||
].join("\n")
|
||||
post = post_with_body(body)
|
||||
|
||||
ChecklistSyntaxMigrator.new(post).update_syntax!
|
||||
|
||||
expected = [
|
||||
'[quote="markvanlan, post:10, topic:10"]',
|
||||
"[\*] This won't be converted",
|
||||
"[/quote]",
|
||||
"[x] That will",
|
||||
'[quote="markvanlan, post:11, topic:10"]',
|
||||
"[\*] Again this won't",
|
||||
"[/quote]",
|
||||
].join("\n")
|
||||
expect(post.reload.raw).to eq(expected)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user