Преглед изворни кода

Add `replace_title` write rule to adjust entry titles

Magnus Åhall пре 2 година
родитељ
комит
69738bce84
2 измењених фајлова са 23 додато и 0 уклоњено
  1. 7 0
      internal/reader/rewrite/rewriter.go
  2. 16 0
      internal/reader/rewrite/rewriter_test.go

+ 7 - 0
internal/reader/rewrite/rewriter.go

@@ -91,6 +91,13 @@ func applyRule(entryURL string, entry *model.Entry, rule rule) {
 		} else {
 			logger.Debug("[Rewrite] Cannot find search and replace terms for replace rule %s", rule)
 		}
+	case "replace_title":
+		// Format: replace_title("search-term"|"replace-term")
+		if len(rule.args) >= 2 {
+			entry.Title = replaceCustom(entry.Title, rule.args[0], rule.args[1])
+		} else {
+			logger.Debug("[Rewrite] Cannot find search and replace terms for replace rule %s", rule)
+		}
 	case "remove":
 		// Format: remove("#selector > .element, .another")
 		if len(rule.args) >= 1 {

+ 16 - 0
internal/reader/rewrite/rewriter_test.go

@@ -450,6 +450,22 @@ func TestRewriteReplaceCustom(t *testing.T) {
 	}
 }
 
+func TestRewriteReplaceTitleCustom(t *testing.T) {
+	controlEntry := &model.Entry{
+		Title:   `Ouch, a thistle`,
+		Content: `The replace_title rewrite rule should not modify the content.`,
+	}
+	testEntry := &model.Entry{
+		Title:   `A title`,
+		Content: `The replace_title rewrite rule should not modify the content.`,
+	}
+	Rewriter("https://example.org/article", testEntry, `replace_title("(?i)^a\\s*ti"|"Ouch, a this")`)
+
+	if !reflect.DeepEqual(testEntry, controlEntry) {
+		t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
+	}
+}
+
 func TestRewriteRemoveCustom(t *testing.T) {
 	controlEntry := &model.Entry{
 		Title:   `A title`,