Skip to content

Commit

Permalink
Introduce md2jcr to the helix importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Helleman committed Dec 10, 2024
1 parent bf34a66 commit 9322c99
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"devDependencies": {
"@adobe/eslint-config-helix": "2.0.8",
"@adobe/helix-md2jcr": "0.0.1",
"@adobe/helix-docx2md": "1.6.11",
"@adobe/helix-mediahandler": "2.5.40",
"@esm-bundle/chai": "4.3.4-fix.0",
Expand Down
19 changes: 18 additions & 1 deletion src/importer/HTML2x.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function html2x(
url,
doc,
transformCfg,
config = { toMd: true, toDocx: false },
config = { toMd: true, toDocx: false, toJcr: false },
params = {},
) {
const transformer = transformCfg || {};
Expand Down Expand Up @@ -156,12 +156,14 @@ async function html2x(
storageHandler,
skipDocxConversion: !config.toDocx,
skipMDFileCreation: !config.toMd,
skipJcrFileCreation: !config.toJcr,
logger,
mdast2docxOptions: {
stylesXML: config.docxStylesXML,
image2png: config.image2png,
},
createDocumentFromString: config.createDocumentFromString,
components: params.components,
});

const pirs = await importer.import(url);
Expand Down Expand Up @@ -191,6 +193,10 @@ async function html2x(
const docx = await storageHandler.get(pir.docx);
res.docx = docx;
}
if (config.toJcr && pir.jcr) {
const jcr = await storageHandler.get(pir.jcr);
res.jcr = jcr;
}
return res;
};

Expand Down Expand Up @@ -247,7 +253,18 @@ async function html2docx(url, document, transformCfg, config, params = {}) {
return html2x(url, doc, transformCfg, { ...config, toMd: true, toDocx: true }, params);
}

async function md2jcr(url, document, transformCfg, config, params = {}) {
let doc = document;
if (typeof doc === 'string') {
doc = parseStringDocument(document, config);
}
return html2x(url, doc, transformCfg, {
...config, toMd: true, toDocx: false, toJcr: true,
}, params);
}

export {
md2jcr,
html2md,
html2docx,
defaultTransformDOM,
Expand Down
13 changes: 13 additions & 0 deletions src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { defaultHandlers, toMdast } from 'hast-util-to-mdast';
import stringify from 'remark-stringify';
import fs from 'fs-extra';
import { md2docx } from '@adobe/helix-md2docx';
import { md2jcr } from '@adobe/helix-md2jcr';
import remarkGridTable from '@adobe/remark-gridtables';
import {
imageReferences,
Expand Down Expand Up @@ -73,6 +74,11 @@ export default class PageImporter {
return this.params.storageHandler.put(docxPath, buffer);
}

async convertToJcr(jcrPath, content) {
const buffer = await md2jcr(content, this.params.components);
return this.params.storageHandler.put(jcrPath, buffer);
}

async createMarkdown(resource, url) {
const { name } = resource;
const { directory } = resource;
Expand Down Expand Up @@ -358,6 +364,13 @@ export default class PageImporter {
// eslint-disable-next-line no-param-reassign
entry.docx = docxPath;
}

if (!this.params.skipJcrFileCreation) {
const jcrPath = `${res.path}.xml`;
await this.convertToJcr(jcrPath, res.content);
// eslint-disable-next-line no-param-reassign
entry.jcr = jcrPath;
}
}

results.push(entry);
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Utils from './utils/Utils.js';

import WPUtils from './wp/WPUtils.js';

import { html2md, html2docx } from './importer/HTML2x.js';
import { html2md, html2docx, md2jcr } from './importer/HTML2x.js';

import createMetadata from './importer/defaults/rules/createMetadata.js';
import adjustImageUrls from './importer/defaults/rules/adjustImageUrls.js';
Expand Down Expand Up @@ -54,5 +54,6 @@ export {
WPUtils,
html2md,
html2docx,
md2jcr,
rules,
};

0 comments on commit 9322c99

Please sign in to comment.