Skip to content

Commit

Permalink
fix edge case with filename creation and duplicate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
kbravh committed Jul 16, 2022
1 parent 5104479 commit 9d10a74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,13 @@ export const createFilename = (
): string => {
filename = filename ? filename : '[[handle]] - [[id]]'
filename = filename.replace(/\.md$/, '') // remove md extension if provided
filename = filename.replace('[[name]]', tweet.includes.users[0].name)
filename = filename.replace('[[handle]]', tweet.includes.users[0].username)
filename = filename.replace('[[id]]', tweet.data.id)
filename = filename.replace('[[text]]', tweet.data.text)
filename = filename.replace(/\[\[name\]\]/gi, tweet.includes.users[0].name)
filename = filename.replace(
/\[\[handle\]\]/gi,
tweet.includes.users[0].username
)
filename = filename.replace(/\[\[id\]\]/gi, tweet.data.id)
filename = filename.replace(/\[\[text\]\]/gi, tweet.data.text)
// date
const dateRegex = /\[\[(date[:\w-]*)\]\]/
if (dateRegex.test(filename)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ describe('Create filename', () => {
})
).toBe('Mappletons - 1292845757297557505 - Maggie Appleton 🧭.md')
})
it('Replaces multiple occurrences of placeholders', () => {
expect(
createFilename(ImageTweet, '[[id]] - [[id]]', {
locale: 'en',
format: 'YYYY-MM-DD',
})
).toBe('1292845757297557505 - 1292845757297557505.md')
})
it('Replaces text and truncates', () => {
expect(
createFilename(ImageTweet, '[[text]]', {
Expand Down

0 comments on commit 9d10a74

Please sign in to comment.