Skip to content

feat: documents link support #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2025
Merged
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
4 changes: 2 additions & 2 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@simplepdf/web-embed-pdf",
"version": "1.8.3",
"version": "1.8.4",
"description": "SimplePDF straight into your website",
"repository": {
"type": "git",
Expand Down
15 changes: 12 additions & 3 deletions web/src/__tests__/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,31 @@ describe('getSimplePDFElements', () => {
<a href="https://pdfobject.com/pdf/sample-3pp.pdf">PDF link</a>
<a href="https://example.com/some-pdf-without-extension" class="simplepdf">Regular link with class</a>
<button class="simplepdf">Button with class</button>
<a href="https://yourcompany.simplepdf.com/form/d8d57ec7-f3e9-4fc9-8cc5-4a92c02d30d0" class="simplepdf">SimplePDF form link</a>
<a href="https://yourcompany.simplepdf.com/form/d8d57ec7-f3e9-4fc9-8cc5-4a92c02d30d0">SimplePDF form link</a>
<a href="https://yourcompany.simplepdf.com/documents/d8d57ec7-f3e9-4fc9-8cc5-4a92c02d30d0">SimplePDF document link</a>
<!--Should NOT detect below-->
<a href="https://pdfobject.com/pdf/sample-3pp.pdf" class="exclude-simplepdf">PDF link with class exclusion</a>
<a href="https://yourcompany.simplepdf.com/form/d8d57ec7-f3e9-4fc9-8cc5-4a92c02d30d0" class="exclude-simplepdf">SimplePDF form link with exclusion</a>
<a href="https://www.pdfsomething.com/anything">Regular link containing .pdf</a>
<a href="https://www.website.com/some-pdf">Should not be opened with SimplePDF</a>
<a href="https://www.website.com/some-other.pdf.png">Should not be opened with SimplePDF</a>
<a href="https://www.simplepdf.app/s/article/How-to-Manage-PDF-Settings">Should not be opened with SimplePDF</a>
<a href="https://www.app.pdf/some-url">Should not be opened with SimplePDF</a>
</body>
</html>
`,
{ url: 'http://localhost' },
);
const detectedElements = getSimplePDFElements(dom.window.document);
expect(detectedElements).toHaveLength(4);
expect(detectedElements).toHaveLength(5);
expect(detectedElements.map(({ innerHTML }) => innerHTML)).toStrictEqual(
expect.arrayContaining(['Button with class', 'PDF link', 'Regular link with class', 'SimplePDF form link']),
expect.arrayContaining([
'Button with class',
'PDF link',
'Regular link with class',
'SimplePDF form link',
'SimplePDF document link',
]),
);
});
});
6 changes: 3 additions & 3 deletions web/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const editorContext: EditorContext = {
},
};

const isFormLink = (url: string) => {
const regex = /^https:\/\/[^.]+\.simplepdf\.com\/[^\/]+\/form\/.+/;
const isSimplePDFLink = (url: string) => {
const regex = /^https:\/\/[^.]+\.simplepdf\.com(\/[^\/]+)?\/(form|documents)\/.+/;
return regex.test(url);
};

Expand Down Expand Up @@ -122,7 +122,7 @@ export const getSimplePDFElements = (document: Document): Element[] => {
return false;
}

return isPDFLink(anchor.href) || anchor.classList.contains('simplepdf') || isFormLink(anchor.href);
return isPDFLink(anchor.href) || anchor.classList.contains('simplepdf') || isSimplePDFLink(anchor.href);
});

return anchorsWithPDF;
Expand Down