Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Restore "fix: nested lists roundtrip (#530)" #556

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/html2md.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
TYPE_GRID_TABLE, TYPE_GT_BODY, TYPE_GT_CELL, TYPE_GT_ROW, handleTableAsGridTable,
} from './mdast-table-handler.js';
import formatPlugin from './markdownFormatPlugin.js';
import { unspreadLists } from './unspread-lists.js';

function m(type, children, props = {}) {
return {
Expand Down Expand Up @@ -300,6 +301,7 @@ export async function html2md(html, opts) {
imageReferences(mdast);
sanitizeHeading(mdast);
sanitizeTextAndFormats(mdast);
unspreadLists(mdast);

// noinspection JSVoidFunctionReturnValueUsed
const md = unified()
Expand Down
26 changes: 26 additions & 0 deletions src/unspread-lists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { visit, CONTINUE } from 'unist-util-visit';

/**
* Collapse (un-spread) all lists
* @param {object} tree
*/
export function unspreadLists(tree) {
visit(tree, (node) => {
if (node.type === 'list' || node.type === 'listItem') {
// eslint-disable-next-line no-param-reassign
node.spread = false;
}
return CONTINUE;
});
}
4 changes: 0 additions & 4 deletions test/fixtures/codeblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
| Procedure |
+--------------------------------------------------------------------------------------------------------------------------------------------------+
| 1. Log in to Azure with the Azure CLI. |
| |
| 2. Query Azure for your tenant and subscription IDs. . |
| |
| ``` |
| az account list |
| ``` |
| |
| 3. Copy the output which is similar to the following example. In the output, identify the `tenantId` tenant ID and the `id` of the subscription. |
| |
| ```bash |
| "cloudName": "AzureCloud", |
| "homeTenantId": <This value is not needed>, |
Expand Down
27 changes: 27 additions & 0 deletions test/roundtrip/roundtrip-fixtures/nested-lists.input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>

<head>
<title>Nested Lists</title>
</head>

<body>
<main>
<div>
<ul>
<li><a href="#" title="Navigate to Text 1">Text 1</a></li>
<li><a href="#" title="Navigate to Text 2">Text 2</a>
<ul>
<li><a href="#" title="Navigate to Text 2 / 1">Text 2 / 1</a>
<ul>
<li><a href="#" title="Navigate to Text 2 / 1 / 1">Text 2 / 1 / 1</a></li>
<li><a href="#" title="Navigate to Text 2 / 1 / 2">Text 2 / 1 / 2</a></li>
<li><a href="#" title="Navigate to Text 2 / 1 / 3">Text 2 / 1 / 3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</main>
</body>
36 changes: 36 additions & 0 deletions test/roundtrip/roundtrip-fixtures/nested-lists.output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Nested Lists</title>
<link rel="canonical" href="https://undefined/">
<meta property="og:title" content="Nested Lists">
<meta property="og:url" content="https://undefined/">
<meta property="og:image" content="https://undefined/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://undefined/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Nested Lists">
<meta name="twitter:image" content="https://undefined/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
</head>
<body>
<header></header>
<main>
<div>
<ul>
<li><a href="#" title="Navigate to Text 1">Text 1</a></li>
<li><a href="#" title="Navigate to Text 2">Text 2</a>
<ul>
<li><a href="#" title="Navigate to Text 2 / 1">Text 2 / 1</a>
<ul>
<li><a href="#" title="Navigate to Text 2 / 1 / 1">Text 2 / 1 / 1</a></li>
<li><a href="#" title="Navigate to Text 2 / 1 / 2">Text 2 / 1 / 2</a></li>
<li><a href="#" title="Navigate to Text 2 / 1 / 3">Text 2 / 1 / 3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</main>
<footer></footer>
</body>
</html>
1 change: 1 addition & 0 deletions test/roundtrip/roundtrip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const specs = [
'some-text',
'all-sections',
'microdata',
'nested-lists',
];

describe('Roundtrip tests', () => {
Expand Down
Loading