Skip to content

Commit

Permalink
Including html forms in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jalagari committed Jan 19, 2024
1 parent a149671 commit 2f7535c
Show file tree
Hide file tree
Showing 10 changed files with 1,738 additions and 14 deletions.
666 changes: 653 additions & 13 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@adobe/helix-shared-wrap": "2.0.0",
"@adobe/helix-status": "10.0.11",
"@adobe/remark-gridtables": "2.0.2",
"@aemforms/forms-importer": "^0.0.1",
"hast-util-select": "6.0.2",
"hast-util-to-mdast": "10.1.0",
"hast-util-to-string": "3.0.0",
Expand Down
10 changes: 10 additions & 0 deletions src/html2md.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TYPE_GRID_TABLE, TYPE_GT_BODY, TYPE_GT_CELL, TYPE_GT_HEADER, TYPE_GT_ROW, handleTableAsGridTable,
} from './mdast-table-handler.js';
import formatPlugin from './markdownFormatPlugin.js';
import { getForms, processForms } from './process-forms.js';

function m(type, children, props = {}) {
return {
Expand Down Expand Up @@ -175,6 +176,13 @@ function createBlocks(main) {
cell.tagName = 'td';
numCols += 1;
tableRow.children.push(cell);
} else if (cell.tagName === 'form') {
const formWrapper = {
type: 'element',
tagName: 'td',
children: [cell],
};
tableRow.children.push(formWrapper);
}
}
maxCols = Math.max(maxCols, numCols);
Expand Down Expand Up @@ -235,6 +243,7 @@ function handleFormat(type) {
export async function html2md(html, opts) {
const { log, url, mediaHandler } = opts;
const t0 = Date.now();
const forms = await getForms(html, opts);
const hast = unified()
.use(parse)
.parse(html);
Expand All @@ -248,6 +257,7 @@ export async function html2md(html, opts) {
processIcons(main);
createSections(main);
createBlocks(main);
processForms(main, forms);

const mdast = toMdast(main, {
handlers: {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async function run(request, ctx) {
mediaHandler,
log,
url,
reqHeaders,
});

const headers = {
Expand Down
65 changes: 65 additions & 0 deletions src/process-forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2023 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.
*/

/* eslint-disable no-param-reassign */
import { visit } from 'unist-util-visit';
import { getAllForms } from '@aemforms/forms-importer';

export async function getForms(html, options) {
const { log } = options;
const formNameDefMap = {};
try {
const forms = await getAllForms(html);
forms?.forEach((form) => {
formNameDefMap[form.name] = form;
});
} catch (e) {
// Ignore errors
log.warn(`Failed to retrieve forms from content ': ${e.message}`);
}
return formNameDefMap;
}

export function processForms(tree, formNameDefMap) {
visit(tree, 'element', (node) => {
if (node.tagName === 'form') {
const name = node?.properties?.id || node?.properties?.name;
const formDef = formNameDefMap[name];
if (formDef) {
node.tagName = 'block';
node.properties = {};
node.children = [
{
type: 'element',
tagName: 'tr',
children: [{
type: 'element',
tagName: 'td',
children: [{
type: 'element',
tagName: 'pre',
children: [{
type: 'text',
tagName: 'code',
value: JSON.stringify(formDef),
}],
}],
}],
},
];
node.data = {
type: 'Form',
};
}
}
});
}
Loading

0 comments on commit 2f7535c

Please sign in to comment.