Skip to content

feat: extract wikilink images #265

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 1 commit into from
Dec 5, 2023
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
14 changes: 13 additions & 1 deletion src/service/extract-img/apply-replace-list.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { ImgInfo } from '@/service/extract-img/get-replace-list'
import { RsText } from '@/wasm'
import { FILTER_BYTE_OFFSET } from './find-img-link'
import { escapeRegExp } from 'lodash'

export function applyReplaceList(
text: string,
replaceList: [src: ImgInfo, newLink: string][],
beforeEach: (newLink: string) => void
) {
const rsCandidate = replaceList.filter(x => x[0].byteOffset !== FILTER_BYTE_OFFSET)

// replace from end
const sorted = replaceList.sort((a, b) => b[0].byteOffset - a[0].byteOffset)
const sorted = rsCandidate.sort((a, b) => b[0].byteOffset - a[0].byteOffset)
for (const [src, newLink] of sorted) {
beforeEach(newLink)
const start = src.byteOffset
const end = src.byteOffset + Buffer.from(src.data).length
text = RsText.replaceWithByteOffset(text, start, end, newLink)
}

const tsCandidate = replaceList.filter(x => x[0].byteOffset === FILTER_BYTE_OFFSET)
for (const [src, newLink] of tsCandidate) {
const prefix = src.prefix ?? ''
const regex = new RegExp(escapeRegExp(String.raw`${prefix}${src.data}`), 'g')
text = text.replace(regex, prefix + newLink)
}

return text
}
1 change: 1 addition & 0 deletions src/service/extract-img/extract-img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export async function extractImg(text: string, fileDir: string, inputImgSrc?: Im
})
)
)

const replaceListLen = replaceList.length

return window.withProgress(
Expand Down
27 changes: 21 additions & 6 deletions src/service/extract-img/find-img-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ const imgTagDataUrlImgPat = r`(<img.*?src\s*=\s*")(${dataUrlPat})"[^/]*?\/?>`
const mkdUrlImgPat = r`(!\[[^]]*\]\()([^) ]+).*?\)`
const imgTagUrlImgPat = r`(<img\s*.*?src\s*=\s*["'])(.*?)["'][^>]*?>`
const mkdDataUrlImgPat = r`(!\[.*?]\()(${dataUrlPat})\)`
const cnbDomain = r`\.cnblogs\.com\/`
const wikilinkImages = /!\[(\[.+?\])\][\s\S]+?(?<prefix>\1:\s*)(?<uri>.*?)\s+/g
const exludeDomains = /\.cnblogs\.com/i
const webUrlPrefix = /^https?:\/\//i

export const FILTER_BYTE_OFFSET = -9999

function getImagesWithTs(text: string) {
return [...text.matchAll(wikilinkImages)].map(m => {
const uri = m.groups?.uri ?? ''
return <ImgInfo>{
byteOffset: FILTER_BYTE_OFFSET,
data: uri,
src: webUrlPrefix.test(uri) ? ImgSrc.web : ImgSrc.fs,
prefix: m.groups?.prefix,
}
})
}

export function findImgLink(text: string): ImgInfo[] {
const imgTagUrlImgMgs = RsRegex.matches(imgTagUrlImgPat, text) as RsMatch[]
Expand All @@ -24,7 +40,7 @@ export function findImgLink(text: string): ImgInfo[] {
const byteOffset = mg.byte_offset + Buffer.from(prefix).length

let src
if (/https?:\/\//.test(data)) src = ImgSrc.web
if (webUrlPrefix.test(data)) src = ImgSrc.web
else src = ImgSrc.fs

return <ImgInfo>{
Expand All @@ -48,8 +64,7 @@ export function findImgLink(text: string): ImgInfo[] {
}
})

const acc = urlImgInfo.concat(dataUrlImgInfo)

// keep links while not cnb
return acc.filter(x => !RsRegex.isMatch(cnbDomain, x.data.toLowerCase()))
let images = urlImgInfo.concat(dataUrlImgInfo)
images = images.concat(getImagesWithTs(text))
return images.filter(x => !exludeDomains.test(x.data))
}
1 change: 1 addition & 0 deletions src/service/extract-img/get-replace-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ImgInfo = {
byteOffset: number
data: string
src: ImgSrc
prefix: string | undefined
}

export const enum ImgSrc {
Expand Down