Commit f26146af authored by Athanasios Katsadas's avatar Athanasios Katsadas 🔎
Browse files

make parser async

parent 75fdb59e
Pipeline #15301 passed with stage
in 2 minutes and 41 seconds
......@@ -25,8 +25,8 @@ function getImportStatement(filePath) {
return importTerm
}
function getHoverText(filePath) {
let data = fs.readFileSync(filePath, 'utf8')
async function getHoverText(filePath) {
let data = await fs.promises.readFile(filePath, 'utf8')
let { metadata } = parseMD(data)
return metadata.hoverText
}
......@@ -41,46 +41,50 @@ var getDirectories = function (src, callback) {
glob(src, callback);
};
function parser(err, files) {
async function parser(err, files) {
if (err) {
console.log('Error', err);
} else {
// Iterate through the .md(x) files
for(let filepath of files.filter(filepath => filepath != './docs/terminology-plugin-instructions.md')) {
fs.readFile(filepath, 'utf8', function(err, content) {
if (err) throw err;
// Regex for finding the pattern: %token_1,token_2%
reg = /\%%.*?\|.*?\%%/g;
// If there is at least one match between the content of the file and
// the regex, proceed
if ((regex_matches = content.match(reg)) !== null) {
for(let regex_match of regex_matches) {
var token = regex_match.split('|');
// Find the path of the term
var reference = (token[1]).replace(/[%" ]/g, '');
var text = (token[0]).replace(/[%"]/g, '');
let referencePath = TERMS_DIR + reference + '.md';
// Get the popup text for the term
let hoverText = getHoverText(referencePath);
var new_text = ('<Term popup="' + hoverText + '" reference="' +
reference + '">' + text + '</Term>');
content = content.replace(regex_match, new_text);
}
// Find the index of the 2nd occurrence of '---'
var indexOfSecond = content.indexOf(searchTerm, 1);
// Add the import statement
var importStatement = getImportStatement(filepath);
content = content.insert(indexOfSecond + 3, importStatement);
// Write the file with the updated content
fs.writeFile(filepath, content, 'utf8', function (err) {
if (err) return console.log(err);
});
let content = '';
try {
content = await fs.promises.readFile(filepath, 'utf8')
} catch(err) {
console.log(err)
}
if (err) throw err;
// Regex for finding the pattern: %token_1,token_2%
reg = /\%%.*?\|.*?\%%/g;
// If there is at least one match between the content of the file and
// the regex, proceed
if ((regex_matches = content.match(reg)) !== null) {
for(let regex_match of regex_matches) {
var token = regex_match.split('|');
// Find the path of the term
var reference = (token[1]).replace(/[%" ]/g, '');
var text = (token[0]).replace(/[%"]/g, '');
let referencePath = TERMS_DIR + reference + '.md';
// Get the popup text for the term
let hoverText = await getHoverText(referencePath);
var new_text = ('<Term popup="' + hoverText + '" reference="' +
reference + '">' + text + '</Term>');
content = content.replace(regex_match, new_text);
}
});
// Find the index of the 2nd occurrence of '---'
var indexOfSecond = content.indexOf(searchTerm, 1);
// Add the import statement
var importStatement = getImportStatement(filepath);
content = content.insert(indexOfSecond + 3, importStatement);
// Write the file with the updated content
fs.writeFile(filepath, content, 'utf8', function (err) {
if (err) return console.log(err);
});
}
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment