Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
telegramHandler,
unknownHandler,
zoomHandler,
mediumHandler,
} from './platforms';
import { DeepLinkResult } from './types';
import { normalizeUrl } from './utils/normalizeUrl';
Expand All @@ -39,6 +40,7 @@ const handlers = [
twitchHandler,
telegramHandler,
zoomHandler,
mediumHandler,
];
export function generateDeepLink(url: string): DeepLinkResult {
const webUrl = normalizeUrl(url);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/platforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { facebookHandler } from './facebook';
import { githubHandler } from './github';
import { instagramHandler } from './instagram';
import { linkedinHandler } from './linkedin';
import { mediumHandler } from './medium';
import { pinterestHandler } from './pinterest';
import { redditHandler } from './reddit';
import { snapchatHandler } from './snapchat';
Expand Down Expand Up @@ -34,4 +35,5 @@ export {
twitchHandler,
unknownHandler,
zoomHandler,
mediumHandler,
};
61 changes: 61 additions & 0 deletions packages/core/src/platforms/medium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { DeepLinkHandler } from '../types';

/**
* Medium URL formats:
* - https://medium.com/@username/article-slug
* - https://medium.com/publication-name/article-slug
* - https://username.medium.com/article-slug
*/
export const mediumHandler: DeepLinkHandler = {
match: (url) => {
const userProfilePattern = /medium\.com\/@([^/?#]+)(?:\/([^/?#]+))?/;

const publicationPattern = /medium\.com\/([^/@?#]+)\/([^/?#]+)/;

const customDomainPattern = /([^/]+)\.medium\.com\/([^/?#]+)/;

let match = url.match(userProfilePattern);
if (match) {
return match as RegExpMatchArray;
}

match = url.match(publicationPattern);
if (match) {
// Return array with [full match, publication name, article slug]
return match as RegExpMatchArray;
}

match = url.match(customDomainPattern);
if (match) {
// Return array with: [full match, username, article slug]
return match as RegExpMatchArray;
}

return null;
},
build: (webUrl, match) => {
let path = '';
try {
const urlObj = new URL(webUrl);
path = urlObj.pathname; // "/@username/article-slug" or "/publication/article-slug"
} catch {
const pathMatch = webUrl.match(/(?:medium\.com|\.medium\.com)(\/[^?#]*)/);
path = pathMatch ? pathMatch[1] : '/';
}

if (!path.startsWith('/')) {
path = '/' + path;
}

const iosDeepLink = `medium://${path}`;
Comment thread
kashyap-savaliya marked this conversation as resolved.

const androidDeepLink = `intent://${path}#Intent;scheme=medium;package=com.medium.reader;S.browser_fallback_url=${encodeURIComponent(webUrl)};end`;

return {
webUrl,
ios: iosDeepLink,
android: androidDeepLink,
platform: 'medium',
};
},
};
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type Platform =
| 'telegram'
| 'zoom'
| 'substack'
| 'medium'
| 'unknown';

export interface DeepLinkResult {
Expand Down