Skip to content

Commit

Permalink
Merge pull request #1056 from AletheiaFact/hotfix-report-page-bug
Browse files Browse the repository at this point in the history
Production Hotfix: Made editor-parser function flexible from different schemas
  • Loading branch information
thesocialdev authored Oct 17, 2023
2 parents a88cd87 + 9b24ed0 commit 22118a4
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions lib/editor-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class EditorParser {
}

getSourceByProperty(sources, property) {
//FIXME: Create migration
return sources.filter(
(source) => (source?.props?.field || source?.field) === property
);
Expand All @@ -48,14 +47,13 @@ export class EditorParser {
}

extractHtmlContentFromRange({ props, content }, type = "text") {
const { textRange, targetText, sup } = props;
const fragmentText = content.slice(...textRange);
const fragmentText = content.slice(...props?.textRange);
if (type === "text") {
return fragmentText;
}

if (type === "source" && fragmentText === targetText) {
return `<a href='#${fragmentText}' rel='noopener noreferrer nofollow'>${fragmentText}<sup>${sup}</sup></a>`;
if (type === "source" && fragmentText === props?.targetText) {
return `<a href='#${fragmentText}' rel='noopener noreferrer nofollow'>${fragmentText}<sup>${props?.sup}</sup></a>`;
}
return fragmentText;
}
Expand All @@ -67,15 +65,19 @@ export class EditorParser {
content
);

const htmlContent = allRanges.map(({ props, type }) => {
return this.extractHtmlContentFromRange(
{
content,
props,
},
type
);
});
const htmlContent = allRanges.map(
({ targetText, textRange, sup, props, type }) => {
return this.extractHtmlContentFromRange(
{
content,
props: props
? { ...props }
: { targetText, textRange, sup },
},
type
);
}
);

if (key === "questions") {
return htmlContent.join("");
Expand Down Expand Up @@ -283,7 +285,9 @@ export class EditorParser {
}

getRawSourcesAndSourcesRanges(sources) {
const rawSourcesRanges = sources.map(({ props }) => props.textRange);
const rawSourcesRanges = sources.map(
(s) => s?.props?.textRange || s?.textRange
);

const sourcesRanges = sources.map((source) => {
return {
Expand All @@ -302,8 +306,7 @@ export class EditorParser {
{ props, content, href = null },
type = "text"
) {
const { textRange, targetText, id } = props;
const fragmentText = content.slice(...textRange);
const fragmentText = content.slice(...props?.textRange);

switch (type) {
case "text":
Expand All @@ -312,11 +315,11 @@ export class EditorParser {
}
break;
case "source":
if (fragmentText === targetText) {
if (fragmentText === props?.targetText) {
return this.getContentObjectWithMarks(
fragmentText,
href,
id
props?.id
);
}
// Fall through to the default case if type is "source" and the text doesn't match targetText
Expand Down Expand Up @@ -400,7 +403,9 @@ export class EditorParser {
});

return [...sourcesRanges, ...missingTextRanges].sort((a, b) => {
return a.props.textRange[0] - b.props.textRange[0];
const getRange = (item) =>
item?.textRange || item?.props?.textRange || [0];
return getRange(a)[0] - getRange(b)[0];
});
}

Expand All @@ -415,10 +420,12 @@ export class EditorParser {
);

const textFragments = allRanges
.map(({ props, type, href }) => {
.map(({ targetText, textRange, props, type, href, id = null }) => {
return this.extractContentFragmentFromRange(
{
props,
props: props
? { ...props }
: { targetText, textRange, id },
content,
href,
},
Expand Down

0 comments on commit 22118a4

Please sign in to comment.