mammoth.js icon indicating copy to clipboard operation
mammoth.js copied to clipboard

Allow ignoring class when finding parent elements that satisfy html path

Open hmnd opened this issue 3 years ago • 0 comments

Issue

Currently, there is no way to instruct mammoth to ignore classes when finding a parent element to nest within. This presents a problem when trying to nest elements within elements that have varying classes set using style maps.

Example

With this style map:

{
  styleMap: [
    "p:ordered-list(1).list-type-decimal => ol.decimal > li:fresh",
    "p:ordered-list(2).list-type-decimal => ol|ul > li > ol.decimal > li:fresh",
    "p:ordered-list(1).list-type-lower-alpha => ol.alpha > li:fresh",
    "p:ordered-list(2).list-type-lower-alpha => ol|ul > li > ol.alpha > li:fresh",
    "p:ordered-list(1).list-type-roman-numeral => ol.roman> li:fresh",
    "p:ordered-list(2).list-type-roman-numeral => ol|ul > li > ol.roman> li:fresh",
  ],
}

This file would be expected to render like so (assuming the classes set the list-style-type with CSS):

<ol class="decimal">
  <li>
    Test
    <ol class="alpha">
      <li>
        Test
        <ol class="roman">
          <li>Test</li>
        </ol>
      </li>
    </ol>
  </li>
</ol>
<p>Test</p>
<p>Test</p>
<p>Test</p>
1. Test
    a. Test
        i. Test
Test
    Test
        Test

However, because the ol.alpha is not nested within an ol and our ol has a class applies to it, a new parent element is created. The document is actually rendered like so:

<ol class="decimal">
  <li>Test</li>
</ol>
<ol>
  <li>
    <ol class="alpha">
      <li>Test</li>
    </ol>
    <ul>
      <li>
        <ol class="roman">
          <li>Test</li>
        </ol>
      </li>
    </ul>
  </li>
</ol>
<p>Test</p>
<p>Test</p>
<p>Test</p>

1. Test
1.    1. Test
      •    i. Test
Test
    Test
        Test

hmnd avatar Jan 19 '22 11:01 hmnd