Skip to content

Commit

Permalink
refactor(pdf): link binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Nov 5, 2021
1 parent fd8d71c commit 0818e6b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
63 changes: 61 additions & 2 deletions src/use/raw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity } from '@/types/context'
import { useUtils } from './utils'

export const bold = () => {
const open = () => {
Expand Down Expand Up @@ -404,10 +405,68 @@ export const useRaw = () => {

const pdf = (raw: string): Array<any> => {
const final: Array<any> = []
let set: false | 'bold' | 'italic' = false

const rest = raw.split(useUtils().regex().htmlTags())

rest.forEach((content: string) => {
// italic
if (set === 'italic') {
final.push({
text: content,
italics: true,
})
set = false
return
}

if (content === html().italic().open()) {
set = 'italic'
return
}

if (set === 'bold') {
final.push({
text: content,
bold: true,
})
set = false
return
}

// bold
if (content === html().bold().open()) {
set = 'bold'
return
}

const rest = raw.split(new RegExp(/<[^<>]+>/g)).join()
if (
content === html().italic().close() ||
content === html().bold().close()
)
return

// http
if (content.match(useUtils().regex().links())) {
const fin = raw.split(useUtils().regex().links())

fin.forEach((str: string) => {
if (str.match(useUtils().regex().links())) {
final.push({
text: str.replace('http://', '').replace('https://', ''),
link: str,
decoration: 'underline',
})
} else {
final.push(str)
}
})

return
}

final.push(rest)
final.push(content)
})

return final
}
Expand Down
16 changes: 15 additions & 1 deletion src/use/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,19 @@ export const useUtils = () => {
return { getSelection }
}

return { delay, prevent, array, join, text }
const regex = () => {
const links = () => {
return new RegExp(
/(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi
)
}

const htmlTags = () => {
return new RegExp(/(<[^<>]+>)/g)
}

return { links, htmlTags }
}

return { delay, prevent, array, join, text, regex }
}

0 comments on commit 0818e6b

Please sign in to comment.