-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (119 loc) · 3.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { docx2md } from '@adobe/helix-docx2md';
import getHast from '@adobe/helix-html-pipeline/src/utils/mdast-to-hast.js';
import splitSections from '@adobe/helix-html-pipeline/src/steps/split-sections.js';
import getMetadata from '@adobe/helix-html-pipeline/src/steps/get-metadata.js';
import unwrapSoleImages from '@adobe/helix-html-pipeline/src/steps/unwrap-sole-images.js';
import html from '@adobe/helix-html-pipeline/src/steps/make-html.js';
import rewriteUrls from '@adobe/helix-html-pipeline/src/steps/rewrite-urls.js';
import rewriteIcons from '@adobe/helix-html-pipeline/src/steps/rewrite-icons.js';
import fixSections from '@adobe/helix-html-pipeline/src/steps/fix-sections.js';
import createPageBlocks from '@adobe/helix-html-pipeline/src/steps/create-page-blocks.js';
import createPictures from '@adobe/helix-html-pipeline/src/steps/create-pictures.js';
import addHeadingIds from '@adobe/helix-html-pipeline/src/steps/add-heading-ids.js';
import render from '@adobe/helix-html-pipeline/src/steps/render.js';
import removeHlxProps from '@adobe/helix-html-pipeline/src/steps/removeHlxProps.js';
import extractMetadata from '@adobe/helix-html-pipeline/src/steps/extract-metadata.js';
import stringify from '@adobe/helix-html-pipeline/src/steps/stringify-response.js';
import parseMarkdown from '@adobe/helix-html-pipeline/src/steps/parse-markdown.js';
import { readFileSync, writeFileSync, watchFile } from 'node:fs';
import { toHtml } from 'hast-util-to-html';
import GithubSlugger from 'github-slugger';
import { execSync } from 'child_process';
import liveserver from 'live-server';
function setDummyMetadata(state) {
state.content = {
...state.content,
meta: {
title: '',
description: '',
url:'',
image: '',
imageAlt: '',
modified_time: '',
section: '',
published_time: '',
modified_time: '',
'twitter:card': {},
}
}
}
async function docx2html(doc, opts) {
console.info('Converting Docx to html');
const md = await docx2md(doc, {});
const state = {
content: {
data: md,
slugger: new GithubSlugger(),
},
info: {
selector: 'notplain',
path: '',
},
metadata: {
getModifiers: (...args) => ({}),
},
mappedMetadata: {
getModifiers: (...args) => ({}),
},
log: {
warn: (...args) => console.warn(args)
},
config: {
host: 'localhost'
},
helixConfig: {
head: {
data: {
html:`
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="/scripts/fallback.js" nomodule></script>
<script src="/scripts/scripts.js" type="module"></script>
<style>body { display: none; }</style>
<link rel="icon" href="data:,">
`
}
}
}
};
const req = { url: {pathname: ''}};
const res = { document: {} };
await parseMarkdown(state);
await splitSections(state);
await getMetadata(state);
await unwrapSoleImages(state);
await html(state);
await rewriteUrls(state);
await rewriteIcons(state);
await fixSections(state);
await createPageBlocks(state);
await createPictures(state);
setDummyMetadata(state);
await extractMetadata(state, req);
await addHeadingIds(state)
await render(state, req, res);
await removeHlxProps(state, req, res);
await stringify(state, req, res);
console.info('done');
return res.body;
}
const [docx] = process.argv.slice(2);
const doTheThing = () => {
const file = readFileSync(docx);
docx2html(file, {}).then(r => {
writeFileSync('helpx-internal/out.html', r);
});
}
execSync('rm -rf helpx-internal');
execSync('git clone -b stage https://github.com/adobecom/helpx-internal.git', {
stdio: [0, 1, 2], // we need this so node will print the command output
cwd: '.', // path to where you want to save the file
});
doTheThing();
liveserver.start({
root: 'helpx-internal',
file: 'out.html',
});
watchFile(docx ,() => {
console.log('Change Detected');
doTheThing();
});