Skip to content

Commit

Permalink
feat(syndicator-mastodon): add option to include permalink in status
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Oct 31, 2023
1 parent fbfdc2d commit b3d92d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
15 changes: 8 additions & 7 deletions packages/syndicator-mastodon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ When sharing content to Mastodon using this syndicator, any post visibility sett

## Options

| Option | Type | Description |
| :--------------- | :-------- | :------------------------------------------------------------------------------------------------------------ |
| `accessToken` | `string` | Your Mastodon access token. _Required_, defaults to `process.env.MASTODON_ACCESS_TOKEN`. |
| `url` | `string` | Your Mastodon server, i.e. `https://mastodon.social`. _Required_. |
| `user` | `string` | Your Mastodon username (without the `@`). _Required_. |
| `characterLimit` | `number` | Maximum number of characters before a post is truncated. _Optional_, defaults to `500`. |
| `checked` | `boolean` | Tell a Micropub client whether this syndicator should be enabled by default. _Optional_, defaults to `false`. |
| Option | Type | Description |
| :----------------- | :-------- | :------------------------------------------------------------------------------------------------------------ |
| `accessToken` | `string` | Your Mastodon access token. _Required_, defaults to `process.env.MASTODON_ACCESS_TOKEN`. |
| `url` | `string` | Your Mastodon server, i.e. `https://mastodon.social`. _Required_. |
| `user` | `string` | Your Mastodon username (without the `@`). _Required_. |
| `characterLimit` | `number` | Maximum number of characters before a post is truncated. _Optional_, defaults to `500`. |
| `checked` | `boolean` | Tell a Micropub client whether this syndicator should be enabled by default. _Optional_, defaults to `false`. |
| `includePermalink` | `boolean` | Always include a link to the original post. _Optional_, defaults to `false`. |
2 changes: 2 additions & 0 deletions packages/syndicator-mastodon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const defaults = {
accessToken: process.env.MASTODON_ACCESS_TOKEN,
characterLimit: 500,
checked: false,
includePermalink: false,
};

export default class MastodonSyndicator {
Expand Down Expand Up @@ -88,6 +89,7 @@ export default class MastodonSyndicator {
return await mastodon({
accessToken: this.options.accessToken,
characterLimit: this.options.characterLimit,
includePermalink: this.options.includePermalink,
serverUrl: `${this.#url.protocol}//${this.#url.hostname}`,
}).post(properties, publication.me);
} catch (error) {
Expand Down
8 changes: 7 additions & 1 deletion packages/syndicator-mastodon/lib/mastodon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { getCanonicalUrl, isSameOrigin } from "@indiekit/util";
import { createRestAPIClient } from "masto";
import { createStatus, getStatusIdFromUrl } from "./utils.js";

export const mastodon = ({ accessToken, characterLimit, serverUrl }) => ({
export const mastodon = ({
accessToken,
characterLimit,
includePermalink,
serverUrl,
}) => ({
client() {
return createRestAPIClient({
accessToken,
Expand Down Expand Up @@ -138,6 +143,7 @@ export const mastodon = ({ accessToken, characterLimit, serverUrl }) => ({

const status = createStatus(properties, {
characterLimit,
includePermalink,
mediaIds,
serverUrl,
});
Expand Down
15 changes: 12 additions & 3 deletions packages/syndicator-mastodon/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { htmlToText } from "html-to-text";
* @param {object} properties - A JF2 properties object
* @param {object} [options] - Options
* @param {number} [options.characterLimit] - Character limit
* @param {number} [options.includePermalink] - Always include permalink
* @param {Array} [options.mediaIds] - Mastodon media IDs
* @param {string} [options.serverUrl] - Server URL
* @returns {object} Status parameters
*/
export const createStatus = (properties, options = {}) => {
const { characterLimit, mediaIds, serverUrl } = options;
const { characterLimit, includePermalink, mediaIds, serverUrl } = options;
const parameters = {};

let status;
Expand All @@ -36,13 +37,21 @@ export const createStatus = (properties, options = {}) => {

// Truncate status if longer than 500 characters
if (status) {
parameters.status = brevity.shorten(
const statusText = brevity.shorten(
status,
properties.url,
false, // https://indieweb.org/permashortlink
includePermalink // https://indieweb.org/permashortlink
? properties.url
: false,
false, // https://indieweb.org/permashortcitation
characterLimit,
);

// Show permalink below status, not within brackets
parameters.status = statusText.replace(
`(${properties.url})`,
`\n\n${properties.url}`,
);
}

// Add media IDs
Expand Down

0 comments on commit b3d92d4

Please sign in to comment.