Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Keep the Substack body off the video embed and read share links

Claude720e09dc0861
diff --git a/server/services/musicVideo/publish/payloads.js b/server/services/musicVideo/publish/payloads.js
index b4c85a7d6d..44c4004b61 100644
--- a/server/services/musicVideo/publish/payloads.js
+++ b/server/services/musicVideo/publish/payloads.js
@@ -86,8 +86,14 @@ const instagramSafe = (caption) => caption.replace(/@(\w)/g, '$1');
* name.substack.com; a custom domain or a pasted URL keeps only its host.
*/
function substackPublication(value) {
- const host = text(value).toLowerCase().replace(/^https?:\/\//, '').replace(/^@/, '').split(/[/?#]/)[0];
+ const address = text(value).toLowerCase().replace(/^https?:\/\//, '');
+ // A share link (open.substack.com/pub/name/p/…) names the publication in its path.
+ const shared = address.match(/^open\.substack\.com\/pub\/([a-z0-9-]{1,63})(?:[/?#]|$)/)?.[1];
+ if (shared) return `${shared}.substack.com`;
+ const host = address.split(/[/?#]/)[0];
if (/^[a-z0-9-]{1,63}$/.test(host)) return `${host}.substack.com`;
+ // substack.com itself (a profile link like substack.com/@name) is not a publication.
+ if (/^(?:(?:www|open)\.)?substack\.com$/.test(host)) return null;
return /^(?:[a-z0-9-]{1,63}\.)+[a-z]{2,}$/.test(host) ? host : null;
}
diff --git a/server/services/musicVideo/publish/payloads.test.js b/server/services/musicVideo/publish/payloads.test.js
index e6d8aa8b2c..f3832b6626 100644
--- a/server/services/musicVideo/publish/payloads.test.js
+++ b/server/services/musicVideo/publish/payloads.test.js
@@ -108,6 +108,9 @@ describe('buildPublishPayload (#9282)', () => {
expect(host('https://Example.substack.com/p/old-post')).toBe('example.substack.com');
expect(host('news.example.com')).toBe('news.example.com');
expect(() => host('not a host')).toThrow(/Substack publication/);
+ expect(host('https://open.substack.com/pub/example/p/old-post?r=abc')).toBe('example.substack.com');
+ expect(() => host('https://substack.com/@example')).toThrow(/Substack publication/);
+ expect(() => host('www.substack.com')).toThrow(/Substack publication/);
expect(buildPublishPayload('substack', project({ copy }), { publication: 'example' }))
.toEqual({ publication: 'example.substack.com', videoUrl: 'https://youtu.be/abc', title: 'Song', subtitle: '', body: 'Body' });
expect(() => buildPublishPayload('substack', project({ copy, links: {} }), { publication: 'example' })).toThrow(/publish to YouTube first/);
diff --git a/server/services/musicVideo/publish/substack.js b/server/services/musicVideo/publish/substack.js
index 7e35f7918d..62194bf6c6 100644
--- a/server/services/musicVideo/publish/substack.js
+++ b/server/services/musicVideo/publish/substack.js
@@ -36,7 +36,10 @@ export const substackAdapter = {
});
if (payload.body) {
await step(label, 'write the body', async () => {
- await page.locator(BODY).first().click();
+ // No click: once the URL became an embed, the editor's middle is the
+ // embed, and a click there would send keys to the player or select the
+ // embed for the paste to replace. Refocus the editor and go to its end.
+ await page.evaluate((sel) => document.querySelector(sel)?.focus(), BODY);
await page.keyboard.press('ControlOrMeta+End');
await page.keyboard.press('Enter');
await pasteText(page, BODY, payload.body);
@@ -46,6 +49,7 @@ export const substackAdapter = {
return {
publication: payload.publication, title: payload.title, subtitle: payload.subtitle || null,
video: embedded ? `Embedded: ${payload.videoUrl}` : `Link (not embedded): ${payload.videoUrl}`,
+ saved: 'Substack keeps this under Drafts; each Fill again saves another draft there',
};
},
};
diff --git a/server/services/musicVideo/publish/substack.test.js b/server/services/musicVideo/publish/substack.test.js
new file mode 100644
index 0000000000..506454dcb0
--- /dev/null
+++ b/server/services/musicVideo/publish/substack.test.js
@@ -0,0 +1,53 @@
+/**
+ * Substack adapter order: the video URL is pasted alone (so Substack can turn
+ * it into an embed), then the body goes after it without clicking the
+ * editor, whose middle is the embed by then.
+ */
+import { describe, expect, it } from 'vitest';
+import { substackAdapter } from './substack.js';
+
+function fakePage({ embedded = true, url = 'https://example.substack.com/publish/post' } = {}) {
+ const log = [];
+ const element = (sel) => ({
+ first: () => element(sel),
+ waitFor: async () => {},
+ fill: async (value) => { log.push(['fill', sel, value]); },
+ click: async () => { log.push(['click', sel]); },
+ });
+ const page = {
+ log,
+ goto: async (to) => { log.push(['goto', to]); },
+ url: () => url,
+ waitForTimeout: async () => {},
+ locator: (sel) => element(sel),
+ keyboard: { press: async (key) => { log.push(['press', key]); } },
+ // pasteText passes [selector, text]; focus and the embed check pass the selector alone.
+ evaluate: async (_fn, arg) => {
+ if (Array.isArray(arg)) { log.push(['paste', arg[1]]); return true; }
+ log.push(['evaluate']);
+ return embedded;
+ },
+ };
+ return page;
+}
+
+const payload = { publication: 'example.substack.com', title: 'Example Song', subtitle: 'A line', body: 'Para one\n\nPara two', videoUrl: 'https://youtu.be/abc' };
+
+describe('Substack adapter', () => {
+ it('pastes the video alone, then writes the body at the end without clicking the embed', async () => {
+ const page = fakePage();
+ const summary = await substackAdapter.prepare(page, payload);
+ expect(page.log[0]).toEqual(['goto', 'https://example.substack.com/publish/post?type=newsletter']);
+ const pastes = page.log.filter(([kind]) => kind === 'paste').map(([, text]) => text);
+ expect(pastes).toEqual(['https://youtu.be/abc', 'Para one\n\nPara two']);
+ const afterVideo = page.log.slice(page.log.findIndex(([kind, text]) => kind === 'paste' && text === payload.videoUrl) + 1);
+ expect(afterVideo.some(([kind]) => kind === 'click')).toBe(false);
+ expect(afterVideo.filter(([kind]) => kind === 'press').map(([, key]) => key)).toEqual(['ControlOrMeta+End', 'Enter']);
+ expect(summary).toMatchObject({ publication: 'example.substack.com', title: 'Example Song', video: 'Embedded: https://youtu.be/abc' });
+ });
+
+ it('says when the link stayed plain text, and asks for sign-in when Substack redirects', async () => {
+ await expect(substackAdapter.prepare(fakePage({ embedded: false }), payload)).resolves.toMatchObject({ video: 'Link (not embedded): https://youtu.be/abc' });
+ await expect(substackAdapter.prepare(fakePage({ url: 'https://substack.com/sign-in?redirect=x' }), payload)).rejects.toMatchObject({ code: 'PUBLISH_LOGIN_REQUIRED' });
+ });
+});