Skip to content

Commit

Permalink
Merge pull request #44 from styxlab/ghost-links
Browse files Browse the repository at this point in the history
improve cms link rewrites
  • Loading branch information
styxlab authored Feb 5, 2021
2 parents 0f8d033 + 4ed3e36 commit 90204f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/ghost-normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { PostOrPage } from '@tryghost/content-api'
import { Dimensions, imageDimensions } from '@lib/images'
import { generateTableOfContents } from '@lib/toc'
import { GhostPostOrPage, createNextProfileImagesFromAuthors } from './ghost'
import { parse as urlParse, UrlWithStringQuery } from 'url'

import { processEnv } from '@lib/processEnv'
const { prism, toc, nextImages } = processEnv

const rehype = Rehype().use({ settings: { fragment: true, space: `html`, emitParseErrors: false, verbose: false } })

export const normalizePost = async (post: PostOrPage, cmsUrl: string | undefined, basePath?: string): Promise<GhostPostOrPage> => {
export const normalizePost = async (post: PostOrPage, cmsUrl: UrlWithStringQuery | undefined, basePath?: string): Promise<GhostPostOrPage> => {
if (!cmsUrl) throw Error('ghost-normalize.ts: cmsUrl expected.')
const rewriteGhostLinks = withRewriteGhostLinks(cmsUrl, basePath)

Expand Down Expand Up @@ -47,11 +48,11 @@ export const normalizePost = async (post: PostOrPage, cmsUrl: string | undefined
* Rewrite absolute Ghost CMS links to relative
*/

const withRewriteGhostLinks = (cmsUrl: string, basePath = '/') => (htmlAst: Node) => {
const withRewriteGhostLinks = (cmsUrl: UrlWithStringQuery, basePath = '/') => (htmlAst: Node) => {
visit(htmlAst, { tagName: `a` }, (node: Node) => {
const href = (node.properties as HTMLAnchorElement).href
if (href?.startsWith(cmsUrl)) {
;(node.properties as HTMLAnchorElement).href = href.replace(cmsUrl, basePath).replace('//', '/')
const href = urlParse((node.properties as HTMLAnchorElement).href)
if (href.protocol === cmsUrl.protocol && href.host === cmsUrl.host) {
;(node.properties as HTMLAnchorElement).href = basePath + href.pathname?.substring(1)
}
})

Expand Down
5 changes: 3 additions & 2 deletions lib/ghost.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse as urlParse, UrlWithStringQuery } from 'url'
import GhostContentAPI, { Params, PostOrPage, SettingsResponse, Pagination, PostsOrPages, Tag, Author } from '@tryghost/content-api'
import { normalizePost } from '@lib/ghost-normalize'
import { Node } from 'unist'
Expand Down Expand Up @@ -200,7 +201,7 @@ export async function getPostBySlug(slug: string): Promise<GhostPostOrPage | nul
if (!post) return null

const { url } = await getAllSettings()
result = await normalizePost(post, url)
result = await normalizePost(post, (url && urlParse(url)) || undefined)
} catch (error) {
if (error.response?.status !== 404) throw new Error(error)
return null
Expand All @@ -220,7 +221,7 @@ export async function getPageBySlug(slug: string): Promise<GhostPostOrPage | nul
if (!page) return null

const { url } = await getAllSettings()
result = await normalizePost(page, url)
result = await normalizePost(page, (url && urlParse(url)) || undefined)
} catch (error) {
if (error.response?.status !== 404) throw new Error(error)
return null
Expand Down

1 comment on commit 90204f0

@vercel
Copy link

@vercel vercel bot commented on 90204f0 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.