Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter replace_title to adjust entry titles #2049

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/reader/rewrite/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions internal/reader/rewrite/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
Loading