Skip to content

Commit

Permalink
chore: release [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
lin-stephanie committed Dec 9, 2024
1 parent 1b597b0 commit 2542879
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-squids-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-loader-tweets": minor
---

Add `newlineHandling` option to specify `\n` processing in `text_html` generation
4 changes: 4 additions & 0 deletions packages/astro-loader-tweets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Please note that under the X API V2 free plan, the endpoint is limited to 1 requ
| `tweetIds`* | `string[]` | An array of Tweet IDs to fetch content for. |
| `removeTrailingUrls` | `boolean` (defaults: `true`) | Whether to remove trailing URLs from the tweet text in the generated `text_html` and `text_markdown`, typically used for views or referenced tweets. |
| `linkTextType` | `'domain-path' \| 'display-url'` (defaults: `'display-url'`) | The type of text to display for links when generating `text_html` and `text_markdown`:<br>`'domain-path'`: Displays the link's domain and path.<br>`'display-url'`: Uses the link text as shown in the tweet. |
| `newlineHandling` | `'none' \| 'break' \| 'paragraph'` (defaults: `'none'`) | The way for processing `\n` in `text_html` generation:<br>`'none'`: Keep as is.<br>`'break'`: Replace `\n` with `<br>`.<br>`'paragraph'`: Wrap paragraphs with `<p>` while removing standalone `\n`. |
| `authToken` | `string` (defaults: `'import.meta.env.X_TOKEN'`) | The X app-only Bearer Token for authentication. Defaults to the `X_TOKEN` environment variable. **If configured here, keep confidential and avoid public exposure.** See [how to create one](https://developer.x.com/en/docs/authentication/oauth-2-0/bearer-tokens) and [configure env vars in an Astro project](https://docs.astro.build/en/guides/environment-variables/#setting-environment-variables). |


Expand All @@ -94,3 +95,6 @@ If you need to [customize the collection schema](https://docs.astro.build/en/gui
[jsdocs-href]: https://www.jsdocs.io/package/astro-loader-tweets
[npm-downloads-src]: https://img.shields.io/npm/dm/astro-loader-tweets?style=flat&colorA=080f12&colorB=ef7575
[npm-downloads-href]: https://npmjs.com/package/astro-loader-tweets


<!-- Rendering posts -->
36 changes: 14 additions & 22 deletions packages/astro-loader-tweets/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Tweetv2FieldsParams } from 'twitter-api-v2'
export const defaultConfig = {
removeTrailingUrls: true,
urlTextType: 'display-url' as const,
newlineHandling: 'none' as const,
}

export const TweetsLoaderConfigSchema = z.object({
Expand Down Expand Up @@ -31,10 +32,22 @@ export const TweetsLoaderConfigSchema = z.object({
.enum(['domain-path', 'display-url'])
.default(defaultConfig.urlTextType),

/**
* The way for processing `\n` in `text_html` generation:
* - `'none'`: Keep as is.
* - `'break'`: Replace `\n` with `<br>`.
* - `'paragraph'`: Wrap paragraphs with `<p>` while removing standalone `\n`.
*
* @default 'none'
*/
newlineHandling: z
.enum(['none', 'break', 'paragraph'])
.default(defaultConfig.newlineHandling),

/**
* The X app-only Bearer Token for authentication.
*
* @remarks This is optional; by default, it reads from the `X_TOKEN` environment variable.
* This is optional; by default, it reads from the `X_TOKEN` environment variable.
* You may also configure it directly here (not recommended; if you do, ensure it is not exposed
* in public code repositories).
*
Expand All @@ -57,27 +70,16 @@ export const getTweetsApiOptions: Partial<Tweetv2FieldsParams> = {
'tweet.fields': [
'id',
'text',
// 'edit_history_tweet_ids', /* (default, lib missing) */
'attachments',
'author_id',
// 'context_annotations',
'conversation_id',
'created_at',
// 'edit_controls',
'entities',
'geo' /* (lib extra) */,
'in_reply_to_user_id',
'lang',
// 'non_public_metrics', /* (requires user context auth) */
// 'note_tweet', /* (lib extra) */
// 'organic_metrics', /* (requires user context auth) */
// 'possibly_sensitive',
// 'promoted_metrics', /* (requires user context auth) */
'public_metrics',
'referenced_tweets',
// 'reply_settings',
'source' /* (lib extra) */,
// 'withheld',
],
'user.fields': [
'id',
Expand All @@ -87,16 +89,9 @@ export const getTweetsApiOptions: Partial<Tweetv2FieldsParams> = {
'created_at',
'description',
'entities',
// 'location',
// 'most_recent_tweet_id', /* (lib extra) */
// 'pinned_tweet_id',
'profile_image_url',
// 'protected',
'public_metrics',
'url',
// 'verified', /* (deprecated in V2) */
// 'verified_type', /* (lib extra) */
// 'withheld',
],
'place.fields': [
'id',
Expand All @@ -119,9 +114,6 @@ export const getTweetsApiOptions: Partial<Tweetv2FieldsParams> = {
'duration_ms',
'public_metrics',
'variants',
// 'non_public_metrics',
// 'organic_metrics',
// 'promoted_metrics',
],
'poll.fields': [
'id',
Expand Down
27 changes: 20 additions & 7 deletions packages/astro-loader-tweets/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ function escapeHTML(str?: string) {
*/
export function processTweetText(
tweet: z.infer<typeof TweetV2Schema>,
options: {
removeTrailingUrls: z.infer<
typeof TweetsLoaderConfigSchema
>['removeTrailingUrls']
linkTextType: z.infer<typeof TweetsLoaderConfigSchema>['linkTextType']
}
options: Omit<
z.infer<typeof TweetsLoaderConfigSchema>,
'tweetIds' | 'authToken'
>
): z.infer<typeof TweetV2WithRichContentSchema> {
const { removeTrailingUrls, linkTextType } = options
const { removeTrailingUrls, linkTextType, newlineHandling } = options

// const originalText = tweet.text
const entities = tweet.entities || {}
Expand Down Expand Up @@ -191,6 +189,21 @@ export function processTweetText(
if (viewType === 'link') urlForLinkView = lastMatchedUrlEntity.expanded_url
}

// handle newline
if (newlineHandling !== 'none') {
if (newlineHandling === 'break') {
textHtml = textHtml.replaceAll('\n', '<br/ >\n')
} else if (newlineHandling === 'paragraph') {
textHtml = textHtml
.split('\n')
.map((line) => {
const l = line.trim()
if (l.length > 0) return `<p>${l}</p>`
})
.join('')
}
}

return {
...tweet,
text_html: textHtml,
Expand Down

0 comments on commit 2542879

Please sign in to comment.