-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to improve code maintainability:
* Move handler file to handler folder * remove unused package * Remove duplicate code * Add error handling for await * Use promisified versions of fs * Isolate function not related to ssgHandler to it own files * Refactor names
- Loading branch information
Showing
8 changed files
with
349 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* dataProcessing separate data title and content | ||
* @param {string} data | ||
* @return {object} title and content | ||
*/ | ||
const dataProcessing = (data, fileExtension) => { | ||
let dataTreated = { title: '', content: '' }; | ||
|
||
if (fileExtension === '.txt') { | ||
//convert data into an array | ||
data = data.split('\n').map((sentence) => sentence.replace('\r', '')); | ||
|
||
if (data.length >= 3) { | ||
//Check if title exist | ||
if (data[0] && !data[1] && !data[2]) { | ||
dataTreated.title = data[0]; | ||
data = data.slice(3); | ||
} | ||
} | ||
|
||
//Remove empty array and combine sentence together | ||
data.forEach((phase, i) => { | ||
if (!phase) data[i] = '_space_'; | ||
}); | ||
data = data.join('').split('_space_'); | ||
dataTreated.content = data.map((paragraph) => `<p>${paragraph}</p>`); | ||
} else { | ||
// If file is .md, assuming the Markdown syntax is correct, replace each Markdown tag to its HTML equivalent | ||
|
||
// convert --- to <hr> | ||
const hr = new RegExp(/^---$/, 'gm'); | ||
data = data.replaceAll(hr, '<hr>'); | ||
|
||
// Links could have the form of [name](href title) or [name](href) | ||
const links = new RegExp(/\[(.*?)\]\((.+?)(?:\s"(.*?)")?\)/, 'gm'); | ||
data = data.replaceAll( | ||
links, | ||
(match, p1, p2, p3) => | ||
`<a href="${p2}" ${p3 ? `title="${p3}"` : ''}>${p1}</a>`, | ||
); | ||
|
||
const bolds = new RegExp(/\*{2}(.+?)\*{2}/, 'gm'); | ||
data = data.replaceAll(bolds, '<strong>$1</strong>'); | ||
|
||
const italics = new RegExp(/\*{1}(.+?)\*{1}/, 'gm'); | ||
data = data.replaceAll(italics, '<i>$1</i>'); | ||
|
||
data = data.split(/(?:\r?\n)+/).map((paragraph) => { | ||
// heading starts with 1 or more hash sign followed by a white space | ||
const headings = new RegExp(/^\s*(#{1,6})\s+(.+)$/, 'gm'); | ||
if (headings.test(paragraph)) { | ||
return paragraph.replaceAll(headings, (match, hash, title) => { | ||
dataTreated.title = | ||
!dataTreated.title && hash.length === 1 ? title : dataTreated.title; | ||
const tag = `h${hash.length}`; | ||
return `<${tag}>${title}</${tag}>`; | ||
}); | ||
} | ||
if (/^<.*>$/.test(paragraph)) return paragraph; | ||
return `<p>${paragraph}</p>`; | ||
}); | ||
|
||
// in markdown, paragraphs are splitted by one or more New line. | ||
dataTreated.content = data; | ||
} | ||
return dataTreated; | ||
}; | ||
module.exports = { dataProcessing }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const { | ||
generateHtmlTemplate, | ||
generateHtmlMenuTemplate, | ||
} = require('./htmlTemplate'); | ||
const path = require('path'); | ||
const { dataProcessing } = require('./dataProcessing'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* createHtmlFile generateHTML file | ||
* @param {string} fileName | ||
* @param {string} data | ||
* @param {string} stylesheet | ||
* @param {string} outputPath | ||
* @param {string} langCode | ||
*/ | ||
const createHtmlFile = async ( | ||
fileName, | ||
data, | ||
stylesheet, | ||
outputPath, | ||
langCode, | ||
) => { | ||
const extname = path.extname(fileName); | ||
fileName = path.basename(fileName, extname); | ||
|
||
let htmlOption = { | ||
...dataProcessing(data, extname), | ||
style: stylesheet, | ||
extname, | ||
langCode, | ||
}; | ||
|
||
const noSpaceFileName = fileName.replaceAll(' ', '-'); | ||
try { | ||
//Create a new html file | ||
await fs.promises.writeFile( | ||
path.join(`${outputPath}`, `${noSpaceFileName}.html`), | ||
generateHtmlTemplate(htmlOption), | ||
(err) => { | ||
if (err) throw new Error(err); | ||
}, | ||
); | ||
} catch (e) { | ||
throw new Error( | ||
`On write file ${path.join(`${outputPath}`, `${noSpaceFileName}.html`)}`, | ||
); | ||
} | ||
console.log( | ||
`File created -> ${path.join(`${outputPath}`, `${noSpaceFileName}.html`)}`, | ||
); | ||
return path.join(`${outputPath}`, `${fileName}.html`); | ||
}; | ||
|
||
/** | ||
* createHtmlFile generateHTML file | ||
* @param {Array} routeList - Array containing name and url for the file route | ||
* @param {string} stylesheet | ||
* @param {string} outputPath | ||
* @param {string} langCode | ||
*/ | ||
const createIndexHtmlFile = async ( | ||
routeList, | ||
stylesheet, | ||
outputPath, | ||
langCode, | ||
) => { | ||
let htmlOption = { | ||
routeList, | ||
style: stylesheet, | ||
langCode, | ||
}; | ||
try { | ||
//Create a new html file | ||
await fs.promises.writeFile( | ||
path.join(`${outputPath}`, `index.html`), | ||
generateHtmlMenuTemplate(htmlOption), | ||
(err) => { | ||
if (err) throw new Error(err); | ||
}, | ||
); | ||
} catch (e) { | ||
throw new Error( | ||
`On write file ${path.join(`${outputPath}`, `index.html`)}`, | ||
); | ||
} | ||
console.log(`File created -> ${path.join(`${outputPath}`, `index.html`)}`); | ||
}; | ||
|
||
module.exports = { createHtmlFile, createIndexHtmlFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* getAllFiles getting all file of a folder recursively | ||
* @param {string} dirPath | ||
* @param {Array} filesPathList | ||
*/ | ||
const getAllFiles = async (dirPath, filesPathList) => { | ||
const files = await fs.promises.readdir(dirPath); | ||
filesPathList ||= []; | ||
|
||
for (const file of files) { | ||
const fileLstat = await fs.promises.lstat(path.join(dirPath, file)); | ||
if (fileLstat.isDirectory()) { | ||
filesPathList = await getAllFiles( | ||
path.join(dirPath, file), | ||
filesPathList, | ||
); | ||
} else { | ||
const extname = path.extname(file); | ||
if (extname === '.txt' || extname === '.md') | ||
filesPathList.push(path.join(dirPath, file)); | ||
} | ||
} | ||
|
||
return filesPathList; | ||
}; | ||
|
||
module.exports = { getAllFiles }; |
File renamed without changes.
2 changes: 0 additions & 2 deletions
2
applyConfiguration.js → src/utils/handler/readConfiguration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b092da9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: