From acb2c6e6288e102edcb215f7bdc6f0a0ccb7b40e Mon Sep 17 00:00:00 2001 From: buddy-web3 <0buddy.ne@gmail.com> Date: Thu, 11 Jan 2024 19:12:50 +0200 Subject: [PATCH 1/5] fix: Numbered Lists not incremental --- parser/parser.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/parser/parser.js b/parser/parser.js index 65a05ce..2081b8a 100644 --- a/parser/parser.js +++ b/parser/parser.js @@ -94,6 +94,7 @@ export const parseDoc = async (doc, answer) => { inlineObjects: doc.inlineObjects, lists: doc.lists || {}, suggestions: new Map(), // Accumulators for the count and total text length of all suggestions + orderedList:1, }; const { paragraphs, relatedAnswerDocIDs, alternativePhrasings, glossary } = extractDocParts(doc); @@ -105,7 +106,6 @@ export const parseDoc = async (doc, answer) => { } const body = paragraphs.map(parseParagraph(documentContext)).join("\n\n"); - const footnotes = extractFootnotes(documentContext, doc); const md = body + "\n\n" + footnotes; @@ -241,6 +241,7 @@ export const parseParagraph = (documentContext) => (paragraph) => { const listID = pb.listId; const list = documentContext.lists[listID]; const currentLevel = list.listProperties.nestingLevels[nestingLevel]; + const orderedList = documentContext.orderedList // This check is ugly as sin, but necessary because GDocs doesn't actually clearly say "this is an [un]ordered list" anywhere // I think this is because internally, all lists are ordered and it just only sometimes uses glyphs which represent that @@ -249,12 +250,10 @@ export const parseParagraph = (documentContext) => (paragraph) => { currentLevel.hasOwnProperty("glyphType") && currentLevel.glyphType !== "GLYPH_TYPE_UNSPECIFIED"; - // Please forgive me for always using 1. as the sequence number on list items - // It's sorta hard to count them properly so I'm depending on markdown renderers doing the heavy lifting for me. - // Which, in fairness, they're supposed to. - itemMarker = isOrdered ? "1. " : "- "; - leadingSpace = new Array(nestingLevel).fill(" ").join(""); + itemMarker = isOrdered ? (orderedList+". "||"1. ") : "- "; + leadingSpace = new Array(nestingLevel).fill(" ").join(""); + documentContext.orderedList++; return ( leadingSpace + itemMarker + From 19c3bfde6e50266ae6a9a2c12caaca9892203ab7 Mon Sep 17 00:00:00 2001 From: buddy-web3 <0buddy.ne@gmail.com> Date: Thu, 11 Jan 2024 19:19:43 +0200 Subject: [PATCH 2/5] fix: run script prettier --- parser/parser.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/parser/parser.js b/parser/parser.js index 2081b8a..7d2d01d 100644 --- a/parser/parser.js +++ b/parser/parser.js @@ -94,7 +94,7 @@ export const parseDoc = async (doc, answer) => { inlineObjects: doc.inlineObjects, lists: doc.lists || {}, suggestions: new Map(), // Accumulators for the count and total text length of all suggestions - orderedList:1, + orderedList: 1, }; const { paragraphs, relatedAnswerDocIDs, alternativePhrasings, glossary } = extractDocParts(doc); @@ -241,7 +241,7 @@ export const parseParagraph = (documentContext) => (paragraph) => { const listID = pb.listId; const list = documentContext.lists[listID]; const currentLevel = list.listProperties.nestingLevels[nestingLevel]; - const orderedList = documentContext.orderedList + const orderedList = documentContext.orderedList; // This check is ugly as sin, but necessary because GDocs doesn't actually clearly say "this is an [un]ordered list" anywhere // I think this is because internally, all lists are ordered and it just only sometimes uses glyphs which represent that @@ -250,8 +250,7 @@ export const parseParagraph = (documentContext) => (paragraph) => { currentLevel.hasOwnProperty("glyphType") && currentLevel.glyphType !== "GLYPH_TYPE_UNSPECIFIED"; - - itemMarker = isOrdered ? (orderedList+". "||"1. ") : "- "; + itemMarker = isOrdered ? orderedList + ". " || "1. " : "- "; leadingSpace = new Array(nestingLevel).fill(" ").join(""); documentContext.orderedList++; return ( From 7da53481e2312c9368ec7397599e3cb6666e5174 Mon Sep 17 00:00:00 2001 From: buddy-web3 <0buddy.ne@gmail.com> Date: Thu, 11 Jan 2024 19:24:14 +0200 Subject: [PATCH 3/5] fix: update test Context --- parser/__tests__/parser.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/parser/__tests__/parser.test.js b/parser/__tests__/parser.test.js index 035beed..db651b1 100644 --- a/parser/__tests__/parser.test.js +++ b/parser/__tests__/parser.test.js @@ -356,6 +356,7 @@ describe("parseParagraph", () => { ...paragraph, bullet: { nestingLevel: 1, listId: "list-id" }, }; + documentContext.orderedList=1; documentContext.lists["list-id"].listProperties.nestingLevels[1].glyphType = "DECIMAL"; const result = parseParagraph(documentContext)(listItem); From 70b17442d4cf2a34ed59f9d048c752919df871c6 Mon Sep 17 00:00:00 2001 From: buddy-web3 <0buddy.ne@gmail.com> Date: Thu, 11 Jan 2024 19:25:18 +0200 Subject: [PATCH 4/5] fix: update test Context --- parser/__tests__/parser.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/__tests__/parser.test.js b/parser/__tests__/parser.test.js index db651b1..b3c2cb8 100644 --- a/parser/__tests__/parser.test.js +++ b/parser/__tests__/parser.test.js @@ -356,7 +356,7 @@ describe("parseParagraph", () => { ...paragraph, bullet: { nestingLevel: 1, listId: "list-id" }, }; - documentContext.orderedList=1; + documentContext.orderedList = 1; documentContext.lists["list-id"].listProperties.nestingLevels[1].glyphType = "DECIMAL"; const result = parseParagraph(documentContext)(listItem); From 08686e19b579dd2014f21ffb556f05a9bf604b93 Mon Sep 17 00:00:00 2001 From: buddy-web3 <0buddy.ne@gmail.com> Date: Fri, 12 Jan 2024 12:33:21 +0200 Subject: [PATCH 5/5] feat: allow multiple lists in one document --- parser/__tests__/parser.test.js | 6 +++++- parser/parser.js | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/parser/__tests__/parser.test.js b/parser/__tests__/parser.test.js index b3c2cb8..62dbd1a 100644 --- a/parser/__tests__/parser.test.js +++ b/parser/__tests__/parser.test.js @@ -281,6 +281,9 @@ describe("parseParagraph", () => { }, }, }, + orderedList: { + "list-id": 1, + }, }; const paragraph = { @@ -338,6 +341,7 @@ describe("parseParagraph", () => { paragraphStyle: { namedStyleType: "HEADING_1" }, bullet: { nestingLevel: 1, listId: "list-id" }, }; + documentContext.orderedList["list-id"] = 1; const result = parseParagraph(documentContext)(heading); expect(result).toEqual(" - # Hello, world!"); }); @@ -356,7 +360,7 @@ describe("parseParagraph", () => { ...paragraph, bullet: { nestingLevel: 1, listId: "list-id" }, }; - documentContext.orderedList = 1; + documentContext.orderedList["list-id"] = 1; documentContext.lists["list-id"].listProperties.nestingLevels[1].glyphType = "DECIMAL"; const result = parseParagraph(documentContext)(listItem); diff --git a/parser/parser.js b/parser/parser.js index 7d2d01d..128cde5 100644 --- a/parser/parser.js +++ b/parser/parser.js @@ -94,8 +94,14 @@ export const parseDoc = async (doc, answer) => { inlineObjects: doc.inlineObjects, lists: doc.lists || {}, suggestions: new Map(), // Accumulators for the count and total text length of all suggestions - orderedList: 1, + orderedList: {}, }; + let i = Object.keys(documentContext.lists).length; + + while (i--) { + documentContext.orderedList[Object.keys(documentContext.lists)[i]] = 1; + } + const { paragraphs, relatedAnswerDocIDs, alternativePhrasings, glossary } = extractDocParts(doc); @@ -107,7 +113,6 @@ export const parseDoc = async (doc, answer) => { const body = paragraphs.map(parseParagraph(documentContext)).join("\n\n"); const footnotes = extractFootnotes(documentContext, doc); - const md = body + "\n\n" + footnotes; // Take the maximum of each suggestion's insertions and deletions @@ -241,7 +246,7 @@ export const parseParagraph = (documentContext) => (paragraph) => { const listID = pb.listId; const list = documentContext.lists[listID]; const currentLevel = list.listProperties.nestingLevels[nestingLevel]; - const orderedList = documentContext.orderedList; + const orderedList = documentContext.orderedList[listID]; // This check is ugly as sin, but necessary because GDocs doesn't actually clearly say "this is an [un]ordered list" anywhere // I think this is because internally, all lists are ordered and it just only sometimes uses glyphs which represent that @@ -252,7 +257,7 @@ export const parseParagraph = (documentContext) => (paragraph) => { itemMarker = isOrdered ? orderedList + ". " || "1. " : "- "; leadingSpace = new Array(nestingLevel).fill(" ").join(""); - documentContext.orderedList++; + documentContext.orderedList[listID]++; return ( leadingSpace + itemMarker +