misskey-plugins/retrospring-formatter.ais

44 lines
1.4 KiB
Text

/// @ 0.19.0
### {
name: "Retrospring Formatter"
version: "1.1.0"
author: "@bunnybeam@kitsunes.club"
description: "Adds a post form action that formats a retrospring answer."
config: {
format: {
type: "string"
label: "Format"
description: "The format of the post. %[question] - The question. %[answer] - Your answer. %[link] - Link to the answer. %[br] - Line break. %[qb <text> /qb] - Quote block."
default: "%[qb %[question] /qb]%[br]%[answer]%[br]<small>%[link]</small>"
}
}
}
Plugin:register_post_form_action("Retrospring format", @(note, rewrite) {
// Split into components
let link_split = note.text.split("https://")
let link = `https://{link_split.pop()}`
let message = link_split.join("https://")
let parts = message.split(" — ")
// Perform substitutions
var output = Plugin:config.format.replace("%[br]", Str:lf)
output = output.replace("%[link]", link)
output = output.replace("%[answer]", parts[1])
output = output.replace("%[question]", parts[0])
// Format quote blocks
for Math:Infinity {
let start_index = output.index_of("%[qb ")
if start_index < 0 {
break
}
let end_index = output.index_of(" /qb]", start_index)
let content = output.slice(start_index+5, end_index)
let result = `> {content.replace(Str:lf, `{Str:lf}> `)}`
output = `{output.slice(0,start_index)}{result}{output.slice(end_index + 5, output.len)}`
}
rewrite("text", output)
rewrite("cw", "retrospring")
})