Skip to content
Snippets Groups Projects
Commit 1c6379a4 authored by Mark Kane's avatar Mark Kane
Browse files

planning#367 improve parser to handle 3 levels,

create initial import of FAQ data
parent 135d6877
No related branches found
No related tags found
1 merge request!160fit-connect/planning#320 Erweiterung FAQ
......@@ -2,13 +2,7 @@ const fs = require('fs');
const readline = require('readline');
const faq = {
groups: [
{
name: 'FAQs',
anchor: "general",
subgroups: []
}
]
groups: []
};
async function parseFaq() {
......@@ -27,7 +21,16 @@ async function parseFaq() {
if (line.startsWith('# ')) {
const name = line.substring(2);
// new subgroup (should be group, but as of 22-03-22 there is only 1 group in most of md doc)
lastSubgroup = 0;
faq
.groups
.push({ name, anchor: slugify(name), subgroups: [] });
lastGroup = faq.groups.length - 1;
console.log('group, index, subgroups: ', faq.groups[lastGroup], lastGroup, lastSubgroup);
} else if (line.startsWith('## ')) {
const name = line.substring(3);
lastItem = 0;
faq
.groups[lastGroup]
......@@ -35,9 +38,7 @@ async function parseFaq() {
.push({ name, anchor: slugify(name), items: [] });
lastSubgroup = faq.groups[lastGroup].subgroups.length - 1;
// console.log('subgroup, item: ', lastSubgroup, lastItem);
} else if (line.startsWith('## ')) {
// should be subgroup, but as of 22-03-22 there is only 1 group in most of md doc)
console.log('subgroup, item: ', lastSubgroup, lastItem);
} else if (line.startsWith('### ')) {
const title = line.substring(4);
......@@ -52,38 +53,49 @@ async function parseFaq() {
// console.log('subgroup, item: ', lastSubgroup, lastItem);
} else if (!line.startsWith('[')) {
let newLine = line;
// look for markdown URLs
const mdRegex = /\[([^\[]+)\]\((.*?)\)/;
const mdUrl = line.match(mdRegex);
if (mdUrl) {
console.log('mdUrl: ', mdUrl);
newLine = newLine.replace(mdUrl[0],
` <Link to={'${mdUrl[2]}'}>${mdUrl[1]}</Link> `);
} else {
// look for raw URLs
const urlRegex = /(https?:\/\/[^ ]*)/;
const url = newLine.match(urlRegex);
if (url) {
newLine = newLine.replace(url[1],
` <Link to={'${url[1]}'}>${url[1]}</Link> `);
if (newLine.length > 0) {
// look for markdown URLs
const mdRegex = /\[([^\[]+)\]\((.*?)\)/;
const mdUrl = line.match(mdRegex);
if (mdUrl) {
// console.log('mdUrl: ', mdUrl);
newLine = newLine.replace(mdUrl[0],
` <Link to={'${mdUrl[2]}'}>${mdUrl[1]}</Link> `);
} else {
// look for raw URLs
const urlRegex = /(https?:\/\/[^ ]*)/;
const url = newLine.match(urlRegex);
if (url) {
newLine = newLine.replace(url[1],
` <Link to={'${url[1]}'}>${url[1]}</Link> `);
}
}
}
// everything else is text of the last item
let item = faq.groups[lastGroup].subgroups[lastSubgroup].items[lastItem];
faq
.groups[lastGroup]
.subgroups[lastSubgroup]
.items[lastItem]
.content = item.content + newLine;
}
} else if (line.indexOf('[not ready]') >= 0) {
// everything else is text of the last item
let item = faq.groups[lastGroup].subgroups[lastSubgroup].items[lastItem];
// console.log('item: ', item);
faq
.groups[lastGroup]
.subgroups[lastSubgroup]
.items[lastItem]
.content = item.content + newLine;
item.notReady = true;
}
}
// remove notReady items
faq.groups.forEach(g => {
g.subgroups.forEach(sg => {
sg.items = sg.items.filter(i => i.notReady !== true && i.content.length > 0)
})
})
// console.log(faq.groups[0]);
let data = JSON.stringify(faq, null, 2);
fs.writeFileSync('scripts/faq.json', data);
fs.writeFileSync('src/data/faq-parsed.json', data);
}
function slugify(value) {
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment