hanford / remark-slate

Remark plugin to compile Markdown as a slate 0.50+ compatible object.
155 stars 42 forks source link

Ordered lists only serialise items as ".1" #66

Closed leo-petrucci closed 1 year ago

leo-petrucci commented 1 year ago

I've happened upon this part of the serialiser:

    case nodeTypes.listItem:
      // whether it's an ordered or unordered list
      const isOL = chunk && chunk.parentType === nodeTypes.ol_list;
      const treatAsLeaf =
        (chunk as BlockType).children.length === 1 &&
        isLeafNode((chunk as BlockType).children[0]);

      let spacer = "";
      for (let k = 0; listDepth > k; k++) {
        if (isOL) {
          // https://github.com/remarkjs/remark-react/issues/65
          spacer += "   ";
        } else {
          spacer += "  ";
        }
      }
      return `${spacer}${isOL ? "1." : "-"} ${children}${
        treatAsLeaf ? "\n" : ""
      }`;

If the list is ordered it will always start any items with .1:

${isOL ? "1." : "-"}

This seems intentional but I can't figure out why. Any idea on how I could count the actual number?

leo-petrucci commented 1 year ago

My temporary fix is to create a variable outside of the function:

let olNumber = 0

Then, right before returning the list item:

      olNumber += 1;
      return `${spacer}${isOL ? `${olNumber}.` : "-"} ${children}${
        treatAsLeaf ? "\n" : ""
      }`;

And the number gets reset every time there's a new ol_list:


    case nodeTypes.ul_list:
      return `${children}${BREAK_TAG}`;
    case nodeTypes.ol_list:
      olNumber = 0;
      return `${children}${BREAK_TAG}`;

Let me know if this is a good solution and I can create a PR 👍

hanford commented 1 year ago

Technically an ordered list in Markdown doesn't need to have an incremented OL number:

https://spec.commonmark.org/dingus/?text=1.%20foo%0A1.%20bar%0A

leo-petrucci commented 1 year ago

I had absolutely no idea that was the case, sorry for the dumb issue!