-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync-docs
More file actions
executable file
·166 lines (133 loc) · 3.96 KB
/
sync-docs
File metadata and controls
executable file
·166 lines (133 loc) · 3.96 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const root = path.resolve(__dirname);
const readmePath = path.join(root, 'README.md');
const scriptPath = path.join(root, 'mlm-memory');
const htmlPath = path.join(root, 'mlm-memory.html');
function readFile(filePath) {
return fs.readFileSync(filePath, 'utf8').replace(/\r\n/g, '\n');
}
function escapeHtml(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function formatInline(text) {
let escaped = escapeHtml(text);
const codeTokens = [];
escaped = escaped.replace(/`([^`]+)`/g, (_, code) => {
codeTokens.push(`<code class="inline">${code}</code>`);
return `@@CODE${codeTokens.length - 1}@@`;
});
escaped = escaped.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img alt="$1" src="$2">');
escaped = escaped.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
escaped = escaped.replace(/@@CODE(\d+)@@/g, (_, idx) => codeTokens[Number(idx)] || '');
return escaped;
}
function markdownToHtml(markdown) {
const lines = markdown.split('\n');
const out = [];
let paragraph = [];
let inList = false;
let inCode = false;
let codeLines = [];
function flushParagraph() {
if (!paragraph.length) return;
out.push(`<p>${formatInline(paragraph.join(' '))}</p>`);
paragraph = [];
}
function closeList() {
if (!inList) return;
out.push('</ul>');
inList = false;
}
function flushCode() {
if (!inCode) return;
out.push(`<pre><code>${escapeHtml(codeLines.join('\n'))}</code></pre>`);
inCode = false;
codeLines = [];
}
for (const line of lines) {
if (line.startsWith('```')) {
flushParagraph();
closeList();
if (!inCode) {
inCode = true;
codeLines = [];
} else {
flushCode();
}
continue;
}
if (inCode) {
codeLines.push(line);
continue;
}
const trimmed = line.trim();
if (!trimmed) {
flushParagraph();
closeList();
continue;
}
// Allow raw HTML blocks in README (useful for centered banners/badges).
if (trimmed.startsWith('<')) {
flushParagraph();
closeList();
out.push(line);
continue;
}
if (trimmed.startsWith('# ')) {
flushParagraph();
closeList();
out.push(`<h1>${formatInline(trimmed.slice(2))}</h1>`);
continue;
}
if (trimmed.startsWith('## ')) {
flushParagraph();
closeList();
out.push(`<h2>${formatInline(trimmed.slice(3))}</h2>`);
continue;
}
if (trimmed.startsWith('### ')) {
flushParagraph();
closeList();
out.push(`<h3>${formatInline(trimmed.slice(4))}</h3>`);
continue;
}
if (trimmed.startsWith('- ')) {
flushParagraph();
if (!inList) {
out.push('<ul>');
inList = true;
}
out.push(`<li>${formatInline(trimmed.slice(2))}</li>`);
continue;
}
paragraph.push(trimmed);
}
flushParagraph();
closeList();
flushCode();
return out.join('\n');
}
function replaceBetween(content, startMarker, endMarker, replacement) {
const start = content.indexOf(startMarker);
const end = content.indexOf(endMarker);
if (start === -1 || end === -1 || end < start) {
throw new Error(`Missing or invalid marker pair: ${startMarker} ... ${endMarker}`);
}
const before = content.slice(0, start + startMarker.length);
const after = content.slice(end);
return `${before}\n${replacement}\n${after}`;
}
const readme = readFile(readmePath);
const script = readFile(scriptPath);
let html = readFile(htmlPath);
const readmeHtml = markdownToHtml(readme);
const scriptHtml = escapeHtml(script);
html = replaceBetween(html, '<!-- README_INJECT_START -->', '<!-- README_INJECT_END -->', readmeHtml);
html = replaceBetween(html, '<!-- SCRIPT_INJECT_START -->', '<!-- SCRIPT_INJECT_END -->', scriptHtml);
fs.writeFileSync(htmlPath, html);
console.log('Synced README.md and mlm-memory into mlm-memory.html');