Skip to content

Commit

Permalink
feat: improve images
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed Aug 13, 2023
1 parent c32cdf8 commit c85c004
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "surmon.me",
"version": "4.16.2",
"version": "4.17.0",
"description": "Surmon.me blog",
"author": "Surmon",
"license": "MIT",
Expand Down
5 changes: 2 additions & 3 deletions src/components/flow/desktop/item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import { useEnhancer } from '/@/app/enhancer'
import { useIdentityStore } from '/@/stores/identity'
import { getImgProxyPath, ImgProxyFormat } from '/@/transforms/imgproxy'
import { getImgProxyURL, isOriginalStaticURL } from '/@/transforms/url'
import { getImgProxyURL, getStaticPath, isOriginalStaticURL } from '/@/transforms/url'
import { getArticleDetailRoute, getCategoryFlowRoute } from '/@/transforms/route'
import { isOriginalType, isHybridType, isReprintType } from '/@/transforms/state'
import { numberSplit } from '/@/transforms/text'
import API_CONFIG from '/@/config/api.config'
const props = defineProps<{
article: Article
Expand All @@ -32,7 +31,7 @@
}
return getImgProxyURL(
cdnDomain,
getImgProxyPath(url.replace(API_CONFIG.STATIC, ''), {
getImgProxyPath(getStaticPath(url), {
resize: true,
width: 350,
height: 238,
Expand Down
5 changes: 2 additions & 3 deletions src/components/flow/mobile/item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import { useIdentityStore } from '/@/stores/identity'
import { getArticleDetailRoute } from '/@/transforms/route'
import { getImgProxyPath, ImgProxyFormat } from '/@/transforms/imgproxy'
import { getImgProxyURL, isOriginalStaticURL } from '/@/transforms/url'
import { getImgProxyURL, getStaticPath, isOriginalStaticURL } from '/@/transforms/url'
import { isOriginalType, isHybridType, isReprintType } from '/@/transforms/state'
import { numberSplit } from '/@/transforms/text'
import API_CONFIG from '/@/config/api.config'
const props = defineProps<{
article: Article
Expand All @@ -32,7 +31,7 @@
}
return getImgProxyURL(
cdnDomain,
getImgProxyPath(url.replace(API_CONFIG.STATIC, ''), {
getImgProxyPath(getStaticPath(url), {
resize: true,
width: 700,
height: 247,
Expand Down
5 changes: 2 additions & 3 deletions src/pages/article/related.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import { useEnhancer } from '/@/app/enhancer'
import { getArticleDetailRoute } from '/@/transforms/route'
import { getImgProxyPath, ImgProxyFormat } from '/@/transforms/imgproxy'
import { getImgProxyURL, isOriginalStaticURL } from '/@/transforms/url'
import { getImgProxyURL, getStaticPath, isOriginalStaticURL } from '/@/transforms/url'
import { Article } from '/@/interfaces/article'
import API_CONFIG from '/@/config/api.config'
interface Props {
articles?: Article[]
Expand All @@ -26,7 +25,7 @@
}
return getImgProxyURL(
cdnDomain,
getImgProxyPath(url.replace(API_CONFIG.STATIC, ''), {
getImgProxyPath(getStaticPath(url), {
resize: true,
width: 466,
height: 168,
Expand Down
5 changes: 2 additions & 3 deletions src/pages/index/carrousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import { Article } from '/@/interfaces/article'
import { Swiper, SwiperSlide } from '/@/effects/swiper'
import { getArticleDetailRoute } from '/@/transforms/route'
import { getAssetURL, getImgProxyURL, isOriginalStaticURL } from '/@/transforms/url'
import { getAssetURL, getImgProxyURL, getStaticPath, isOriginalStaticURL } from '/@/transforms/url'
import { getImgProxyPath, ImgProxyFormat } from '/@/transforms/imgproxy'
import API_CONFIG from '/@/config/api.config'
interface Props {
articles: Array<Article>
Expand All @@ -34,7 +33,7 @@
}
return getImgProxyURL(
cdnDomain,
getImgProxyPath(url.replace(API_CONFIG.STATIC, ''), {
getImgProxyPath(getStaticPath(url), {
resize: true,
width: 1190,
height: 420,
Expand Down
18 changes: 13 additions & 5 deletions src/stores/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { Article } from '/@/interfaces/article'
import { Pagination, PaginationList } from '/@/interfaces/common'
import { getArticleContentHeadingElementId } from '/@/constants/anchor'
import { markdownToHTML, MarkdownRenderOption } from '/@/transforms/markdown'
import { getCDNPrefixURL, CDNPrefix } from '/@/transforms/url'
import { getStaticURL, getStaticPath, getImgProxyURL, isOriginalStaticURL } from '/@/transforms/url'
import { getImgProxyPath } from '/@/transforms/imgproxy'
import { delayPromise } from '/@/utils/delayer'
import { isClient } from '/@/app/environment'
import { LONG_ARTICLE_THRESHOLD } from '/@/config/app.config'
import nodepress from '/@/services/nodepress'
import API_CONFIG from '/@/config/api.config'

export const ARTICLE_API_PATH = '/article'

Expand Down Expand Up @@ -133,11 +133,19 @@ export const useArticleDetailStore = defineStore('articleDetail', () => {
})

const optimizeImageSource = (src: string) => {
if (src.startsWith(API_CONFIG.STATIC)) {
return src.replace(API_CONFIG.STATIC, getCDNPrefixURL(useCDNDomain(), CDNPrefix.Static))
} else {
if (!isOriginalStaticURL(src)) {
return src
}

const cdnDomain = useCDNDomain()
const path = getStaticPath(src)
return {
src: getStaticURL(cdnDomain, path),
sources: [
{ type: 'image/avif', srcset: getImgProxyURL(cdnDomain, getImgProxyPath(path, { format: 'avif' })) },
{ type: 'image/webp', srcset: getImgProxyURL(cdnDomain, getImgProxyPath(path, { format: 'webp' })) }
]
}
}

const defaultContent = computed(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/transforms/imgproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export interface ImgProxyOptions {
}

export const getImgProxyPath = (path: string, options: ImgProxyOptions) => {
const resize = `resize:fill:${options.width || ''}:${options.height || ''}:0`
const resize = options.resize ? `resize:fill:${options.width || ''}:${options.height || ''}:0` : ''
const watermark = options.watermark ? `/${options.watermark}` : ''
const format = options.format ? `@${options.format}` : ''
return `/${options.resize ? resize : ''}${watermark}/plain${normalizePath(path)}${format}`
return `/${resize}${watermark}/plain${normalizePath(path)}${format}`.replaceAll('//', '/')
}
34 changes: 21 additions & 13 deletions src/transforms/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const trimHTML = (html: string) => html.replace(/\s+/g, ' ').replace(/\n/g, ' ')
interface RendererCreatorOptions {
sanitize: boolean
text: (text: string) => string
imageSource: (src: string) => string
headingId: (html: string, level: number, raw: string) => string
imageSource: (src: string) => string | { src: string; sources: Array<{ srcset: string; type: string }> }
}

const createRenderer = (options?: Partial<RendererCreatorOptions>): Renderer => {
Expand Down Expand Up @@ -135,8 +135,12 @@ const createRenderer = (options?: Partial<RendererCreatorOptions>): Renderer =>
const titleValue = sanitizeHTML(escape(title || alt))
const altValue = sanitizeHTML(escape(alt!))
const sanitized = sanitizeUrl(src!)
const source = sanitized.startsWith('http://') ? getOriginalProxyURL(sanitized) : sanitized
const sourceValue = options?.imageSource ? options.imageSource(source) : source
const original = sanitized.startsWith('http://') ? getOriginalProxyURL(sanitized) : sanitized
const parsed = options?.imageSource ? options.imageSource(original) : original
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/currentSrc
const srcValue = typeof parsed === 'object' ? parsed.src : parsed
const sourcesValue = typeof parsed === 'object' ? parsed.sources : []

// figure > alt
return trimHTML(`
Expand All @@ -151,15 +155,19 @@ const createRenderer = (options?: Partial<RendererCreatorOptions>): Renderer =>
<div></div>
<div></div>
</div>
<img
class="${LOZAD_CLASS_NAME}"
data-src="${sourceValue}"
${altValue ? `alt="${altValue}"` : ''}
${titleValue ? `title="${titleValue}"` : ''}
onload="this.parentElement.dataset.status = 'loaded'"
onerror="this.parentElement.dataset.status = 'error'"
onclick="window.$popup.vImage(this.src)"
/>
<picture>
${sourcesValue.map((s) => `<source srcset="${s.srcset}" type="${s.type}" />`).join('\n')}
<img
class="${LOZAD_CLASS_NAME}"
data-src="${srcValue}"
draggable="false"
${altValue ? `alt="${altValue}"` : ''}
${titleValue ? `title="${titleValue}"` : ''}
onload="this.parentElement.parentElement.dataset.status = 'loaded'"
onerror="this.parentElement.parentElement.dataset.status = 'error'"
onclick="window.$popup.vImage(this.currentSrc || this.src)"
/>
</picture>
${altValue ? `<figcaption>${altValue}</figcaption>` : ''}
</figure>
</div>
Expand Down Expand Up @@ -217,5 +225,5 @@ export const markdownToHTML = (markdown: string, options?: MarkdownRenderOption)
imageSource: options?.imageSourceGetter
}

return marked.parse(markdown, { renderer: createRenderer(renderOptions) })
return marked.parse(markdown, { renderer: createRenderer(renderOptions) }) as string
}
12 changes: 8 additions & 4 deletions src/transforms/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export const getAssetURL = (domain: string, path: string) => {
return isDev ? normalizedPath : `${getCDNPrefixURL(domain, CDNPrefix.Assets)}${normalizedPath}`
}

export const isOriginalStaticURL = (url?: string) => {
return url?.startsWith(API_CONFIG.STATIC)
}

export const getStaticURL = (domain: string, path: string) => {
return `${getCDNPrefixURL(domain, CDNPrefix.Static)}${normalizePath(path)}`
}
Expand All @@ -40,6 +36,14 @@ export const getImgProxyURL = (domain: string, path: string) => {
return `${getCDNPrefixURL(domain, CDNPrefix.ImgProxy)}${normalizePath(path)}`
}

export const isOriginalStaticURL = (url?: string) => {
return url?.startsWith(API_CONFIG.STATIC)
}

export const getStaticPath = (url: string) => {
return url.replace(API_CONFIG.STATIC, '')
}

export const getOriginalProxyURL = (url: string) => {
return `${BFF_PROXY_PREFIX}/${btoa(url)}`
}
Expand Down

0 comments on commit c85c004

Please sign in to comment.