kadikraman / draftjs-md-converter

Converts content from Draft.js blocks to Markdown and vice versa.
https://kadikraman.github.io/draftjs-md-converter/
MIT License
145 stars 37 forks source link

getBlockStyle counter bug #74

Open Lieblein opened 3 years ago

Lieblein commented 3 years ago

Function getBlockStyle give incorrect counter value. For example: draft js mode

I have for this case in md: md result text

But I would see this: md should to be

Original code of getBlockStyle:

const getBlockStyle = (currentStyle, appliedBlockStyles) => {
  if (currentStyle === 'ordered-list-item') {
    const counter = appliedBlockStyles.reduce((prev, style) => {
      if (style === 'ordered-list-item') {
        return prev + 1;
      }
      return prev;
    }, 1);
    return `${counter}. `;
  }
  return blockStyleDict[currentStyle] || '';
};

My version:

const getBlockStyle = (currentStyle, appliedBlockStyles) => {
  if (currentStyle === 'ordered-list-item') {
    let count = 0;
    for (let i = appliedBlockStyles.length - 1; i >= 0; i--) {
      if (appliedBlockStyles[i] !== 'ordered-list-item') {
        break;
      } else {
        count++;
      }
    }
    return `${count + 1}. `;
  }
  return blockStyleDict[currentStyle] || '';
};