Skip to content

Commit

Permalink
add title support
Browse files Browse the repository at this point in the history
  • Loading branch information
aine-etke committed Feb 10, 2025
1 parent 98287ec commit f9c2597
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ A CLI tool that joins the room and exports last N messages to the file you speci
* Get messages from any matrix room with pagination (if limit greather than page, to prevent timeout errors) or without it (if limit less or equals page)
* Export messages to one file for all messages
* Export each message in separate file
* Custom templates supported (`contrib` contains an example of hugo post template, [etke.cc/webite](https://gitlab.com/etke.cc/website) can be used as reference)
* Custom templates supported (`contrib` contains an example of hugo post template, [etke.cc/news](https://etke.cc/news) can be used as reference)
* Delegation and aliases supported
* `Anyone`/`world_readable` access supported without invite

## Usage

### Full example

That's how [etke.cc/website](https://gitlab.com/etke.cc/website) news generated
That's how [etke.cc/news](https://etke.cc/news) generated

```bash
emm -hs hs.url -u user -p pass -r "#room:hs.url" -t contrib/hugo-post-template.md -o /tmp/%s.md
Expand Down
2 changes: 1 addition & 1 deletion contrib/hugo-post-template.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: '{{ .ID }}'
date: '{{ .CreatedAt }}'
title: '{{ .CreatedAt }}'
title: '{{ .Title }}{{ .ReplacedNote }}'
author: '{{ .Author }}'
draft: false
---
Expand Down
1 change: 1 addition & 0 deletions internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// DefaultTemplate text
const DefaultTemplate = `
id={{ .ID }}
title={{ .Title }}
replace={{ .Replace }}
author={{ .Author }}
text={{ .Text }}
Expand Down
22 changes: 22 additions & 0 deletions internal/matrix/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package matrix
import (
"context"
"log"
"strings"
"time"

"maunium.net/go/mautrix"
Expand All @@ -23,6 +24,8 @@ type Message struct {
ReplacedNote string
// Author is a matrix id of the sender
Author id.UserID
// Title is a message title (first line of the message)
Title string
// Text is the message body in plaintext/markdown format
Text string
// HTML is the message body in html format
Expand Down Expand Up @@ -174,13 +177,32 @@ func parseMessage(evt *event.Event) *Message {
}

createdAt := time.UnixMilli(evt.Timestamp).UTC()
title := createdAt.Format("2006-01-02 15:04 MST")
if strings.Contains(text, "\n") {
title = sanitizeTitle(strings.Split(text, "\n")[0])
}

return &Message{
ID: evt.ID,
Replace: replace,
Author: evt.Sender,
Title: title,
Text: text,
HTML: html,
CreatedAt: createdAt.Format("2006-01-02 15:04 MST"),
CreatedAtFull: createdAt,
}
}

func sanitizeTitle(text string) string {
prefixes := []string{"> ", "# ", "```", "~~", "| ", "* ", "- ", "+ ", "1. ", "1) "}
kws := []string{"**", "__", "`", "```", "~~", "||", "'"}
for _, prefix := range prefixes {
text = strings.TrimPrefix(text, prefix)
}
for _, kw := range kws {
text = strings.ReplaceAll(text, kw, "")
}

return text
}

0 comments on commit f9c2597

Please sign in to comment.