Skip to content

Commit

Permalink
Merge pull request #622 from digabi/fix/answer_is_empty
Browse files Browse the repository at this point in the history
Fix/answer is empty
  • Loading branch information
heeboA authored Dec 2, 2024
2 parents 5686491 + 7269c1d commit 3b5008d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/app/state/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createContext, PropsWithChildren, ReactPortal, useContext, useEffect, u
import { Container } from 'react-dom/client'

import useHistory from './history'
import useMap, { MapHookHandle } from '../hooks/use-map'

import MathEditor, { MathEditorHandle, Props as MathEditorProps } from '../components/math-editor'
import { createMathStub } from '../utils/create-math-stub'
Expand Down
12 changes: 6 additions & 6 deletions src/app/utility.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Attributes } from 'sanitize-html'
import { sanitize } from './utils/sanitization'
import { MATH_EDITOR_CLASS } from './utils/create-math-stub'

export const eventHandlerWithoutFocusLoss = (fn?: () => void) => (e: React.MouseEvent) => {
if (fn) {
Expand Down Expand Up @@ -89,7 +90,7 @@ export const getAnswer = (html: string) => {
img: (tagName: string, attribs: Attributes) =>
attribs.src ? { tagName, attribs } : { tagName: '', attribs: {} },
span: (tagName: string, attribs: Attributes) =>
attribs.class !== 'math-editor-wrapper' ? { tagName, attribs } : { tagName: '', attribs: {} },
attribs.class !== MATH_EDITOR_CLASS ? { tagName, attribs } : { tagName: '', attribs: {} },
},
})
const answer = new DOMParser().parseFromString(answerHtml, 'text/html').body
Expand All @@ -100,16 +101,15 @@ export const getAnswer = (html: string) => {
})
.join('')

const screenshots = answer.querySelectorAll(':scope > img:not([src*="/math.svg?"])')
const screenshotCount = answer.querySelectorAll(':scope > img:not([src*="/math.svg?"])').length
const equationCount = answer.querySelectorAll(':scope > img[src*="/math.svg?"]').length

const equationCount = answer.querySelectorAll('span.math-editor-wrapper').length

const isEmpty = answerText?.trim().length === 0 && screenshots.length === 0 && equationCount > 0
const isEmpty = answerText?.trim().length === 0 && screenshotCount === 0 && equationCount === 0

return {
answerHtml: isEmpty ? '' : stripBrsAndTrimFromEnd(answerHtml),
answerText: stripNewLinesFromStartAndWhiteSpacesFromEnd(answerText ?? ''),
imageCount: screenshots.length,
imageCount: screenshotCount,
}
}

Expand Down

0 comments on commit 3b5008d

Please sign in to comment.