Skip to content

Commit

Permalink
fix(imagedelivery): relative path image inlining
Browse files Browse the repository at this point in the history
resolved #147
  • Loading branch information
49659410+tx0c committed Aug 26, 2023
1 parent 814c2c8 commit 9e85818
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 28 deletions.
4 changes: 2 additions & 2 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/ipns-site-generator",
"version": "0.1.2",
"version": "0.1.3",
"description": "IPNS site generator for matters.town",
"author": "https://github.com/thematters <tech@matters.town>",
"homepage": "https://github.com/matters-ipns-site-generator",
Expand Down
24 changes: 12 additions & 12 deletions src/__tests__/__snapshots__/makeArticlePage.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Array [
<meta name=\\"twitter:title\\" content=\\"Excepteur in consequat mollit consectetur.\\">
<meta name=\\"twitter:description\\" content=\\"Aliquip reprehenderit elit nulla commodo sit. Aute deserunt quis cupidatat ea quis. Adipisicing magna tempor esse ea anim. Veniam aliquip laborum aliquip est laborum irure duis irure adipisicing dolore laboris in irure. Non sunt esse cillum consequat. Cupidatat qui ex fugiat ullamco cupidatat eu eu eu labore excepteur. Tempor tempor mollit nulla qui Lorem aute consectetur sint sint. Nostrud cupidatat ullamco ea elit in voluptate do mollit veniam.\\">
<meta name=\\"twitter:image\\" content=\\"https://assets.matters.news/processed/1080w/profileCover/aa57a1ce-8926-4512-81d8-462f68fa3917.webp\\" />
<meta name=\\"keywords\\" content=\\"Matters,matters.news,tag1,tag2,tag3\\" />
<meta name=\\"keywords\\" content=\\"Matters,matters.town,tag1,tag2,tag3\\" />
<link rel=\\"alternate\\" type=\\"application/atom+xml\\" href=\\"../rss.xml\\" data-link-type=\\"rss-xml\\" />
Expand Down Expand Up @@ -566,7 +566,7 @@ article li {
<h1 itemprop=\\"headline\\">Excepteur in consequat mollit consectetur.</h1>
<section class=\\"byline\\" itemprops=\\"provider\\" itemscope itemtype=\\"http://schema.org/Organization\\">
<a href=\\"https://matters.news/@Anim\\" target=\\"_blank\\">
<a href=\\"https://matters.town/@Anim\\" target=\\"_blank\\">
Excepteur Non
</a>
&nbsp;
Expand All @@ -576,7 +576,7 @@ article li {
&nbsp;from&nbsp;
<a href=\\"https://matters.news\\" target=\\"_blank\\">
<a href=\\"https://matters.town\\" target=\\"_blank\\">
Matters
</a>
</section>
Expand All @@ -592,7 +592,7 @@ article li {
<article class=\\"\\" itemprop=\\"articleBody\\">
<figure class=\\"image\\">
<img src=\\"./aa57a1ce-8926-4512-81d8-462f68fa3917.webp\\" data-asset-id=\\"aa57a1ce-8926-4512-81d8-462f68fa3917\\">
<img src=\\"./82cb20da-xxxxx4a73-xxxx3994.jpeg\\">
<figcaption><span>Reprehenderit proident sit consectetur id consequat officia.</span></figcaption>
</figure>
Expand Down Expand Up @@ -785,7 +785,7 @@ Array [
<meta name=\\"twitter:title\\" content=\\"Excepteur in consequat mollit consectetur.\\">
<meta name=\\"twitter:description\\" content=\\"Aliquip reprehenderit elit nulla commodo sit. Aute deserunt quis cupidatat ea quis. Adipisicing magna tempor esse ea anim. Veniam aliquip laborum aliquip est laborum irure duis irure adipisicing dolore laboris in irure. Non sunt esse cillum consequat. Cupidatat qui ex fugiat ullamco cupidatat eu eu eu labore excepteur. Tempor tempor mollit nulla qui Lorem aute consectetur sint sint. Nostrud cupidatat ullamco ea elit in voluptate do mollit veniam.\\">
<meta name=\\"twitter:image\\" content=\\"https://assets.matters.news/processed/1080w/profileCover/aa57a1ce-8926-4512-81d8-462f68fa3917.webp\\" />
<meta name=\\"keywords\\" content=\\"Matters,matters.news,tag1,tag2,tag3\\" />
<meta name=\\"keywords\\" content=\\"Matters,matters.town,tag1,tag2,tag3\\" />
<link rel=\\"alternate\\" type=\\"application/atom+xml\\" href=\\"../rss.xml\\" data-link-type=\\"rss-xml\\" />
Expand Down Expand Up @@ -1333,7 +1333,7 @@ article li {
<h1 itemprop=\\"headline\\">Excepteur in consequat mollit consectetur.</h1>
<section class=\\"byline\\" itemprops=\\"provider\\" itemscope itemtype=\\"http://schema.org/Organization\\">
<a href=\\"https://matters.news/@Anim\\" target=\\"_blank\\">
<a href=\\"https://matters.town/@Anim\\" target=\\"_blank\\">
Excepteur Non
</a>
&nbsp;
Expand All @@ -1343,7 +1343,7 @@ article li {
&nbsp;from&nbsp;
<a href=\\"https://matters.news\\" target=\\"_blank\\">
<a href=\\"https://matters.town\\" target=\\"_blank\\">
Matters
</a>
</section>
Expand All @@ -1359,7 +1359,7 @@ article li {
<article class=\\"\\" itemprop=\\"articleBody\\">
<figure class=\\"image\\">
<img src=\\"./aa57a1ce-8926-4512-81d8-462f68fa3917.webp\\" data-asset-id=\\"aa57a1ce-8926-4512-81d8-462f68fa3917\\">
<img src=\\"./82cb20da-xxxxx4a73-xxxx3994.jpeg\\">
<figcaption><span>Reprehenderit proident sit consectetur id consequat officia.</span></figcaption>
</figure>
Expand Down Expand Up @@ -1552,7 +1552,7 @@ Array [
<meta name=\\"twitter:title\\" content=\\"Excepteur in consequat mollit consectetur.\\">
<meta name=\\"twitter:description\\" content=\\"Aliquip reprehenderit elit nulla commodo sit. Aute deserunt quis cupidatat ea quis. Adipisicing magna tempor esse ea anim. Veniam aliquip laborum aliquip est laborum irure duis irure adipisicing dolore laboris in irure. Non sunt esse cillum consequat. Cupidatat qui ex fugiat ullamco cupidatat eu eu eu labore excepteur. Tempor tempor mollit nulla qui Lorem aute consectetur sint sint. Nostrud cupidatat ullamco ea elit in voluptate do mollit veniam.\\">
<meta name=\\"twitter:image\\" content=\\"https://assets.matters.news/processed/1080w/profileCover/aa57a1ce-8926-4512-81d8-462f68fa3917.webp\\" />
<meta name=\\"keywords\\" content=\\"Matters,matters.news,tag1,tag2,tag3\\" />
<meta name=\\"keywords\\" content=\\"Matters,matters.town,tag1,tag2,tag3\\" />
<link rel=\\"alternate\\" type=\\"application/atom+xml\\" href=\\"../rss.xml\\" data-link-type=\\"rss-xml\\" />
Expand Down Expand Up @@ -2100,7 +2100,7 @@ article li {
<h1 itemprop=\\"headline\\">Excepteur in consequat mollit consectetur.</h1>
<section class=\\"byline\\" itemprops=\\"provider\\" itemscope itemtype=\\"http://schema.org/Organization\\">
<a href=\\"https://matters.news/@Anim\\" target=\\"_blank\\">
<a href=\\"https://matters.town/@Anim\\" target=\\"_blank\\">
Excepteur Non
</a>
&nbsp;
Expand All @@ -2110,7 +2110,7 @@ article li {
&nbsp;from&nbsp;
<a href=\\"https://matters.news\\" target=\\"_blank\\">
<a href=\\"https://matters.town\\" target=\\"_blank\\">
Matters
</a>
</section>
Expand All @@ -2126,7 +2126,7 @@ article li {
<article class=\\"\\" itemprop=\\"articleBody\\">
<figure class=\\"image\\">
<img src=\\"https://assets.matters.news/processed/1080w/profileCover/aa57a1ce-8926-4512-81d8-462f68fa3917.webp\\" data-asset-id=\\"aa57a1ce-8926-4512-81d8-462f68fa3917\\">
<img src=\\"https://imagedelivery.net/kDRCxxxx-pYA/prod/embed/82cb20da-xxxxx4a73-xxxx3994.jpeg/public\\">
<figcaption><span>Reprehenderit proident sit consectetur id consequat officia.</span></figcaption>
</figure>
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/__snapshots__/makeHomepage.test.js.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/__tests__/makeArticlePage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deserializeHtmlBundle } from './utils'
jest.mock('isomorphic-fetch')
const mockedFetch = fetch as jest.Mock

const MOCK_ARTICLE_PAGE_DATA = MOCK_ARTICLE_PAGE('matters.news')
const MOCK_ARTICLE_PAGE_DATA = MOCK_ARTICLE_PAGE('matters.town')

describe('makeArticlePage', () => {
test('can generate basic HTML bundle', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/makeArticlePage/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export default async ({
const parsedUrl = new URL(fullUrl)
if (
Array.isArray(onlyDomains) &&
onlyDomains.some((d) => parsedUrl.hostname.endsWith(domain))
onlyDomains.some((d) => parsedUrl.hostname.endsWith(d))
) {
// to next
// to next fetch'ing
} else if (!parsedUrl.hostname.endsWith(domain)) {
// skip assets from other domain
return
Expand Down
8 changes: 7 additions & 1 deletion src/makeArticlePage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ export const makeArticlePage = async (data: MakeArticlePageData) => {
if ('tagName' in element) {
tagName = element.tagName
}

// e.g. https://imagedelivery.net/kDRCxxx-pYA/prod/embed/82cb20da-9fe0-4a73-bb33-b1d0ef353994.jpeg/public
const isImageDelivery = elementSrc.startsWith('https://imagedelivery.net')
// assuming it's http url
const assetPath =
elementSrc.split('/').pop() || `${index.toString()}-${tagName}`
(isImageDelivery
? elementSrc.split('/').splice(-2).shift() // get the 2nd last section of pathname
: elementSrc.split('/').pop()) || // get the last section of pathname
`${index}-${tagName}`

const updateSrc = () => $(element).attr('src', `./${assetPath}`)

Expand Down
2 changes: 1 addition & 1 deletion src/render/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const MOCK_ARTICLE = (siteDomain = 'matters.town') => ({
sourceUri: `https://${siteDomain}/@${MOCK_AUTHOR.userName}/1-slug-Qmc919SaZGj1yeDCLHx7KMY7WMzy1PF6UjixuapcG1bfAB`,
content: `
<figure class="image">
<img src="https://assets.matters.news/processed/1080w/profileCover/aa57a1ce-8926-4512-81d8-462f68fa3917.webp" data-asset-id="aa57a1ce-8926-4512-81d8-462f68fa3917">
<img src="https://imagedelivery.net/kDRCxxxx-pYA/prod/embed/82cb20da-xxxxx4a73-xxxx3994.jpeg/public">
<figcaption><span>Reprehenderit proident sit consectetur id consequat officia.</span></figcaption>
</figure>
Expand Down

0 comments on commit 9e85818

Please sign in to comment.