diff --git a/parser/__tests__/parser.test.js b/parser/__tests__/parser.test.js index 035beed..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,6 +360,7 @@ describe("parseParagraph", () => { ...paragraph, bullet: { nestingLevel: 1, listId: "list-id" }, }; + 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 65a05ce..128cde5 100644 --- a/parser/parser.js +++ b/parser/parser.js @@ -94,7 +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: {}, }; + let i = Object.keys(documentContext.lists).length; + + while (i--) { + documentContext.orderedList[Object.keys(documentContext.lists)[i]] = 1; + } + const { paragraphs, relatedAnswerDocIDs, alternativePhrasings, glossary } = extractDocParts(doc); @@ -105,9 +112,7 @@ 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,6 +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[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 @@ -249,12 +255,9 @@ 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. " : "- "; + itemMarker = isOrdered ? orderedList + ". " || "1. " : "- "; leadingSpace = new Array(nestingLevel).fill(" ").join(""); - + documentContext.orderedList[listID]++; return ( leadingSpace + itemMarker +