implement title trimming
This commit is contained in:
parent
4d45134e1f
commit
8598a898f4
|
@ -29,13 +29,17 @@ Usage example:
|
|||
await mastodon.parseFeed({
|
||||
notesdir: `${__dirname}/content/notes`,
|
||||
url: "https://chat.brainbaking.com/users/wouter/feed",
|
||||
utcOffset: 60
|
||||
utcOffset: 60,
|
||||
titleCount: 50,
|
||||
titlePrefix: "Note: "
|
||||
})
|
||||
```
|
||||
|
||||
Default values:
|
||||
|
||||
- `utcOffset`: `60` (= GMT+1, that's where I am!) (in **minutes**, see [day.js docs](https://day.js.org/docs/en/manipulate/utc-offset)
|
||||
- `titleCount`: `50`. Will add "..." and trim if title length bigger.
|
||||
- `titlePrefix`: `""`. Will add before title (e.g. "Note: ")
|
||||
|
||||
Note that this **does not** delete the notes dir with every call. It simply checks if there isn't already a file with the same name (based on the publication date), and adds one if not.
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "jam-my-stack",
|
||||
"version": "1.0.7",
|
||||
"version": "1.0.8",
|
||||
"repository": {
|
||||
"url": "https://github.com/wgroeneveld/jam-my-stack",
|
||||
"type": "git"
|
||||
|
|
|
@ -34,13 +34,22 @@ ${item.content}
|
|||
writeFileSync(`${path}/${item.hash}.md`, mddata, 'utf-8')
|
||||
}
|
||||
|
||||
function trimIfNeeded(title, count, prefix) {
|
||||
if(title.length > count) {
|
||||
return prefix + title.substring(0, count) + "..."
|
||||
}
|
||||
return prefix + title
|
||||
}
|
||||
|
||||
// opts:
|
||||
// notesdir = `${__dirname}/content/notes`
|
||||
// url = "https://chat.brainbaking.com/users/wouter/feed";
|
||||
// utcOffset = "+01:00"
|
||||
// utcOffset = 60 (in minutes)
|
||||
// titleCount = 50
|
||||
// titlePrefix = "Note: "
|
||||
|
||||
async function parseMastoFeed(options) {
|
||||
const { notesdir, url, utcOffset = 60 } = options
|
||||
const { notesdir, url, utcOffset = 60, titleCount = 50, titlePrefix = "" } = options
|
||||
|
||||
const notesroot = await getFiles(notesdir)
|
||||
const notes = notesroot
|
||||
|
@ -65,7 +74,7 @@ async function parseMastoFeed(options) {
|
|||
const context = item['thr:in-reply-to'] ? item['thr:in-reply-to']['@_ref'] : ""
|
||||
|
||||
return {
|
||||
title: ent.decode(item.title), // summary (cut-off) of content
|
||||
title: trimIfNeeded(ent.decode(item.title), titleCount, titlePrefix), // summary (cut-off) of content
|
||||
content: ent.decode(item.content['#text']), // format: <span class="h-card....
|
||||
url: item.id, // format: https://chat.brainbaking.com/objects/0707fd54-185d-4ee7-9204-be370d57663c
|
||||
context,
|
||||
|
|
|
@ -18,6 +18,35 @@ describe("mastodon feed parser tests", () => {
|
|||
fs.mkdirSync(dumpdir)
|
||||
});
|
||||
|
||||
test("parse trims title according to config and adds three dots", async () => {
|
||||
await parseMastoFeed({
|
||||
url: "invalid",
|
||||
notesdir: dumpdir,
|
||||
utcOffset: 0,
|
||||
titleCount: 5,
|
||||
titlePrefix: "Note: "
|
||||
})
|
||||
|
||||
const actualMd = await fsp.readFile(`${dumpdir}/2021/03/02h16m18s46.md`)
|
||||
|
||||
const md = frontMatterParser.parseSync(actualMd.toString())
|
||||
expect(md.data.title).toBe("Note: @Stam...")
|
||||
})
|
||||
|
||||
test("parse does not trim if titleCount > title length and does not add three dots", async () => {
|
||||
await parseMastoFeed({
|
||||
url: "invalid",
|
||||
notesdir: dumpdir,
|
||||
utcOffset: 0,
|
||||
titleCount: 5000
|
||||
})
|
||||
|
||||
const actualMd = await fsp.readFile(`${dumpdir}/2021/03/02h16m18s46.md`)
|
||||
|
||||
const md = frontMatterParser.parseSync(actualMd.toString())
|
||||
expect(md.data.title).toBe("@StampedingLonghorn I tried to chase him away, but you know how that turned out... 😼 There's ...")
|
||||
})
|
||||
|
||||
test("parse creates separate notes in each month subdir", async () => {
|
||||
await parseMastoFeed({
|
||||
url: "invalid",
|
||||
|
@ -34,7 +63,8 @@ describe("mastodon feed parser tests", () => {
|
|||
await parseMastoFeed({
|
||||
url: "invalid",
|
||||
notesdir: dumpdir,
|
||||
utcOffset: 0
|
||||
utcOffset: 0,
|
||||
titleCount: 5000
|
||||
})
|
||||
|
||||
const actualMd = (await fsp.readFile(`${dumpdir}/2021/03/01h19m03s35.md`)).toString()
|
||||
|
@ -46,7 +76,8 @@ describe("mastodon feed parser tests", () => {
|
|||
await parseMastoFeed({
|
||||
url: "invalid",
|
||||
notesdir: dumpdir,
|
||||
utcOffset: 0
|
||||
utcOffset: 0,
|
||||
titleCount: 5000
|
||||
})
|
||||
|
||||
const actualMd = await fsp.readFile(`${dumpdir}/2021/03/02h16m18s46.md`)
|
||||
|
|
Loading…
Reference in New Issue