-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (137 loc) · 4.87 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
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
const fs = require('fs-extra');
const marked = require('marked');
const path = require('path');
/**
* Parse a Markdown file and extract its content, title, tags, and HTML representation.
* @param {string} filePath - Path to the Markdown file.
* @returns {Promise<Object>} - Parsed content including title, body, tags, and HTML content.
*/
async function parseMarkdownFile(filePath) {
try {
const content = await fs.readFile(filePath, 'utf-8');
const htmlContent = marked.parse(content);
const fileName = path.basename(filePath, '.md'); // Extract file name without .md extension
let title = null;
let body = content;
let tags = [];
const lines = content.split('\n');
if (lines[0].startsWith('# ')) {
title = lines[0].slice(2).trim(); // Use the first line as the title if it's a Markdown header
body = lines.slice(1).join('\n');
}
// If no title is defined in the file, use the file name
if (!title) {
title = fileName;
}
// Extract tags from lines starting with '- ' or inline hashtags
for (const line of lines) {
if (line.startsWith('- ')) {
tags.push(...line.slice(2).split('-').map(tag => tag.trim()));
}
}
// Include inline tags starting with '#'
const inlineTags = [...content.matchAll(/#(\w+)/g)].map(match => match[1]);
tags.push(...inlineTags);
// Ensure tags are unique and normalized
tags = Array.from(new Set(tags.map(tag => tag.trim())));
return { title, body, tags, htmlContent };
} catch (err) {
console.error(`Error reading file ${filePath}:`, err);
throw err;
}
}
/**
* Generate a JSON file containing facts from Markdown files in a directory.
* @param {string} markdownDir - Directory containing Markdown files.
* @param {string} outputFile - Path to the output JSON file.
*/
async function generateFactsJSON(markdownDir, outputFile) {
try {
const files = await fs.readdir(markdownDir);
const facts = [];
for (const file of files) {
if (path.extname(file) === '.md') {
const filePath = path.join(markdownDir, file);
const fact = await parseMarkdownFile(filePath);
// Exclude files tagged as #UNFINISHED
if (!fact.tags.includes('UNFINISHED')) {
facts.push(fact);
}
}
}
await fs.writeFile(outputFile, JSON.stringify(facts, null, 2));
console.log(`Facts JSON generated at ${outputFile}`);
} catch (err) {
console.error(`Error generating facts JSON:`, err);
}
}
/**
* Generate a JSON file containing rules for json-logic-js based on Markdown files in a directory.
* @param {string} markdownDir - Directory containing Markdown files.
* @param {string} outputFile - Path to the output JSON file.
*/
async function generateRulesJSON(markdownDir, outputFile) {
try {
const files = await fs.readdir(markdownDir);
const rules = new Set(); // Use a set to prevent duplicate rules
for (const file of files) {
if (path.extname(file) === '.md') {
const filePath = path.join(markdownDir, file);
const fact = await parseMarkdownFile(filePath);
// Exclude files tagged as #UNFINISHED
if (!fact.tags.includes('UNFINISHED')) {
// Create rules for json-logic-js based on tags
fact.tags.forEach(tag => {
const ruleKey = JSON.stringify({
conditions: { "in": [tag, { var: "tags" }] },
event: {
type: "tagMatch",
params: {
message: `Matched tag: ${tag}`,
title: fact.title,
body: fact.body
}
}
});
if (!rules.has(ruleKey)) {
rules.add(ruleKey);
}
});
// Create rules for json-logic-js based on body content
const bodyRuleKey = JSON.stringify({
conditions: { "==": [{ var: "body" }, fact.body] },
event: {
type: "bodyMatch",
params: {
message: `Matched body content`,
title: fact.title,
body: fact.body
}
}
});
if (!rules.has(bodyRuleKey)) {
rules.add(bodyRuleKey);
}
}
}
}
// Write unique rules to output file
await fs.writeFile(outputFile, JSON.stringify([...rules].map(rule => JSON.parse(rule)), null, 2));
console.log(`Rules JSON generated at ${outputFile}`);
} catch (err) {
console.error(`Error generating rules JSON:`, err);
}
}
// Configuration
const markdownDir = './markdown';
const factsOutputFile = './facts.json';
const rulesOutputFile = './rules.json';
// Main script execution
(async () => {
try {
await generateFactsJSON(markdownDir, factsOutputFile);
await generateRulesJSON(markdownDir, rulesOutputFile);
} catch (err) {
console.error('Error in script execution:', err);
}
})();