Repository: prettier/prettier
File: src/language-markdown/printer-markdown.js (JavaScript)

Code before:
  if (shouldRemainTheSameContent(path)) {
    /** @type {Doc} */
    const parts = [""];
    const textsNodes = splitText(
      options.originalText.slice(
        node.position.start.offset,
        node.position.end.offset,
      ),
    );
    for (const node of textsNodes) {
      if (node.type === "word") {
        parts.push([parts.pop(), node.value]);
        continue;
      }
      const doc = printWhitespace(path, node.value, options.proseWrap, true);
      if (getDocType(doc) === DOC_TYPE_STRING) {
        parts.push([parts.pop(), doc]);
        continue;
      }
      parts.push(doc, "");
    }
    return fill(parts);
  }

  switch (node.type) {
    case "front-matter":
      return options.originalText.slice(
        node.position.start.offset,
        node.position.end.offset,
      );
    case "root":
      /* c8 ignore next 3 */
      if (node.children.length === 0) {
        return "";
      }
      return [printRoot(path, options, print), hardline];
    case "paragraph":
      return printParagraph(path, options, print);
    case "sentence":
      return printSentence(path, print);
    case "word": {
      let escapedValue = node.value
        .replaceAll("*", String.raw`\*`) // escape all `*`
        .replaceAll(
          new RegExp(
            [
              `(^|${PUNCTUATION_REGEXP.source})(_+)`,
              `(_+)(${PUNCTUATION_REGEXP.source}|$)`,
            ].join("|"),
            "gu",
          ),

Review comment (suggestion):
`parts` should always satisfy following:
- its length is odd
- element in even index should be line

In this path, `doc` is line. To meet the former rule, we need additional element (`""`).

Code after:
  if (shouldRemainTheSameContent(path)) {
    /*
     * We assume parts always meet following conditions:
     * - parts.length is odd
     * - odd (0-indexed) elements are line-like doc
     */
    /** @type {Doc[]} */
    const parts = [""];
    const textsNodes = splitText(
      options.originalText.slice(
        node.position.start.offset,
        node.position.end.offset,
      ),
    );
    for (const node of textsNodes) {
      if (node.type === "word") {
        parts.push([parts.pop(), node.value]);
        continue;
      }
      const doc = printWhitespace(path, node.value, options.proseWrap, true);
      if (getDocType(doc) === DOC_TYPE_STRING) {
        parts.push([parts.pop(), doc]);
        continue;
      }
      // In this path, doc is line. To meet the condition, we need additional element "".
      parts.push(doc, "");
    }
    return fill(parts);
  }

  switch (node.type) {
    case "front-matter":
      return options.originalText.slice(
        node.position.start.offset,
        node.position.end.offset,
      );
    case "root":
      /* c8 ignore next 3 */
      if (node.children.length === 0) {
        return "";
      }
      return [printRoot(path, options, print), hardline];
    case "paragraph":
      return printParagraph(path, options, print);
    case "sentence":
      return printSentence(path, print);
    case "word": {
      let escapedValue = node.value
        .replaceAll("*", String.raw`\*`) // escape all `*`
        .replaceAll(
          new RegExp(