Skip to content
Merged
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
20 changes: 18 additions & 2 deletions packages/typed-message/dom/Renderer/utils/renderText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,28 @@ export const RenderLinkFragment = memo(function RenderLink(
})

function parseText(string: string, Text: NonNullable<RenderFragmentsContextType['Text']>) {
const links = parseLink(string).flatMap((x, index) => {
const links = parseLink(string).flatMap((x) => {
if (x.type === 'text') {
return x.content.split(/(\n)/g).map((x) => (x === '\n' && index !== 0 ? <br /> : <Text children={x} />))
return sliceString(x.content).map((x) => (x === '\n' ? <br /> : <Text children={x} />))
}
if (x.category === 'normal' && !x.content.match(/^https?:\/\//gi)) x.content = 'http://' + x.content
return <RenderLinkFragment category={x.category} href={x.content} children={x.content} />
})
return links
}

function sliceString(x: string): string[] {
const result: string[] = []

let pos = 0
let index = x.indexOf('\n')

if (index === -1) return [x]
while (index !== -1) {
result.push(x.slice(pos, index), '\n')
pos = index + 1
index = x.indexOf('\n', pos)
}
result.push(x.slice(pos))
return result.filter(Boolean)
}