From 27a5f6862b1748ec10ca9282e0fe1a64f9ccf910 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 14 Jan 2024 22:59:31 +0100 Subject: [PATCH] fix: convert everything which looks like an url to a . + +package migration + +import ( + "strings" + + "mvdan.cc/xurls/v2" + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" +) + +func convertLinksToHTMLElements(input string) (output string) { + + links := xurls.Strict().FindAllString(input, -1) + + if len(links) == 0 { + return input + } + + unique := make(map[string]bool) + for _, link := range links { + unique[link] = true + } + + for link := range unique { + + if strings.Contains(input, `href="`+link) { + continue + } + + input = strings.ReplaceAll(input, link, ``+link+``) + } + + return input +} + +func convertDescriptionToLinks(tx *xorm.Engine, table string, column string) (err error) { + items := []map[string]interface{}{} + err = tx.Table(table). + Select("id, " + column). + Find(&items) + if err != nil { + return + } + + for _, task := range items { + if task[column] == "" || task[column] == "

" { + continue + } + + task[column] = convertLinksToHTMLElements(task[column].(string)) + _, err = tx.Where("id = ?", task["id"]). + Table(table). + Cols(column). + Update(task) + if err != nil { + return + } + } + + return +} + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20240114224713", + Description: "Convert all non-html links to html a href", + Migrate: func(tx *xorm.Engine) (err error) { + + for _, table := range []string{ + "tasks", + "labels", + "projects", + "saved_filters", + "teams", + } { + err = convertDescriptionToLinks(tx, table, "description") + if err != nil { + return + } + } + + err = convertDescriptionToLinks(tx, "task_comments", "comment") + return + }, + Rollback: func(tx *xorm.Engine) error { + return nil + }, + }) +}