Skip to content

Commit

Permalink
Merge pull request #144 from jeffsikes/issue-empty-title
Browse files Browse the repository at this point in the history
Empty title fixes; formatting AP content updates
  • Loading branch information
ckolderup authored Oct 9, 2023
2 parents 983709c + fcd80cf commit 0522da2
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 14 deletions.
5 changes: 4 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express from 'express';
import cors from 'cors';
import { create } from 'express-handlebars';

import { domain, account, simpleLogger, actorInfo } from './src/util.js';
import { domain, account, simpleLogger, actorInfo, replaceEmptyText } from './src/util.js';
import session, { isAuthenticated } from './src/session-auth.js';
import * as bookmarksDb from './src/bookmarks-db.js';
import * as apDb from './src/activity-pub-db.js';
Expand Down Expand Up @@ -97,6 +97,9 @@ const hbs = create({
eq(a, b, options) {
return a === b ? options.fn(this) : options.inverse(this);
},
setTitle(item) {
return replaceEmptyText(item.title, item.url);
},
},
partialsDir: './src/pages/partials',
extname: '.hbs',
Expand Down
9 changes: 5 additions & 4 deletions src/activitypub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fetch from 'node-fetch';
import crypto from 'crypto';

import { signedGetJSON, signedPostJSON } from './signature.js';
import { actorInfo, actorMatchesUsername } from './util.js';
import { actorInfo, actorMatchesUsername, replaceEmptyText } from './util.js';

function getGuidFromPermalink(urlString) {
return urlString.match(/(?:\/m\/)([a-zA-Z0-9+/]+)/)[1];
Expand Down Expand Up @@ -34,9 +34,10 @@ export function createNoteObject(bookmark, account, domain) {
type: 'Note',
published: d.toISOString(),
attributedTo: `https://${domain}/u/${account}`,
content: `
<strong><a href="${bookmark.url}">${bookmark.title}</a></strong><br/>
${bookmark.description?.replace('\n', '<br/>') || ''}`,
content: `<strong><a href="${bookmark.url}" rel="nofollow noopener noreferrer" target="_blank">${replaceEmptyText(
bookmark.title,
bookmark.url,
)}</a></strong><br/>${bookmark.description?.trim().replace('\n', '<br/>') || ''}`,
to: [`https://${domain}/u/${account}/followers/`, 'https://www.w3.org/ns/activitystreams#Public'],
tag: [],
};
Expand Down
4 changes: 2 additions & 2 deletions src/pages/bookmark.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 class="bookmark-permalink-title">
<a href="{{bookmark.url}}">
{{bookmark.title}} <span class="external-link">↗</span>
</a>
{{setTitle bookmark}}<span class="external-link">↗</span>
</a>
</h1>

{{> show_bookmark bookmark=bookmark permalink=true}}
Expand Down
6 changes: 4 additions & 2 deletions src/pages/bookmarks-xml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
</author>
{{#each bookmarks}}
<entry>
<title>{{title}}</title>
<title>{{setTitle this}}</title>
<link rel="alternate" href="{{url}}"/>
<id>{{projectUrl}}/bookmark/{{id}}</id>
<updated>{{created_at}}</updated>
{{#if description}}
{{#if description }}
<summary type="html">{{description}}</summary>
{{ else }}
<summary type="html"> </summary>
{{/if}}
{{#each tag_array}}
<category term="{{this}}" />
Expand Down
4 changes: 3 additions & 1 deletion src/pages/partials/edit_bookmark.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
</label>
<input
id="bookmark_url"
type="text"
type="url"
name="url"
value="{{bookmark.url}}"
required
aria-required="true"
/>
</div>
<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion src/pages/partials/show_bookmark.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{^if permalink}}
<div class="headline">
<a href="{{bookmark.url}}">
{{bookmark.title}}
{{setTitle bookmark}}
</a>
</div>
{{/if}}
Expand Down
6 changes: 3 additions & 3 deletions src/routes/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ router.post('/multiadd', isAuthenticated, async (req, res) => {

await bookmarksDb.createBookmark({
url: link,
title: meta.result.ogTitle,
description: meta.result.ogDescription || ' ', // add *something*, even if ogDesc is empty (keeps Atom feed validation happy)
title: meta.result?.ogTitle,
description: (meta.result && meta.result.ogDescription) || ' ', // add *something*, even if ogDesc is empty (keeps Atom feed validation happy)
});
});

Expand Down Expand Up @@ -248,7 +248,7 @@ router.post('/:id?', isAuthenticated, async (req, res) => {
bookmark = await bookmarksDb.createBookmark({
// STRONG PARAMETERS
url: mergedObject.url.trim(),
title: mergedObject.title?.trim() || 'Untitled',
title: mergedObject.title?.trim(),
description: mergedObject.description?.trim() || '',
tags,
});
Expand Down
7 changes: 7 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export function actorMatchesUsername(actor, username) {
return actorAccount === actorResult[3] && actorDomain === actorResult[1];
}

export function replaceEmptyText(currentValue, defaultValue) {
if (!currentValue || currentValue?.trim().replace(/\n/g, '') === '') {
return defaultValue;
}
return currentValue;
}

export function simpleLogger(req, res, next) {
// middleware function
const currentDatetime = new Date();
Expand Down

0 comments on commit 0522da2

Please sign in to comment.