Skip to content

Commit

Permalink
Refactoring txt2html app to improve code maintainability:
Browse files Browse the repository at this point in the history
	* Get rid of Global Variable

	* Improving variable name in txt2html.js file

	* Change function name in generator.js file
  • Loading branch information
Thanh Van committed Oct 14, 2021
1 parent 89268f4 commit 0173054
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function generateFromText(text, filename, cssHref) {
}

export function generateFromMd(text, filename, cssHref) {
let tags = convertToH1(text);
let tags = convertHeading1(text);
return insertInTemplate(tags, filename, cssHref);
}

Expand Down Expand Up @@ -37,6 +37,6 @@ function splitInParagraphs(text) {
return text;
}

function convertToH1(text) {
function convertHeading1(text) {
return text.replace(/^# (.*$)/gim, "<h1>$1</h1>");
}
14 changes: 7 additions & 7 deletions src/txt2html.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ const argv = yargs(process.argv.slice(2))
},
}).argv;

let data = argv.config ? fileSystem.readFileSync(argv.config) : null;
const config = JSON.parse(data) || null;

async function txt2html(filePath) {
const fileStat = await fs.stat(filePath);
let data = argv.config ? fileSystem.readFileSync(argv.config) : null;
const configData = JSON.parse(data) || null;

if (fileStat.isFile() && path.extname(filePath) == ".txt") {
const text = (await fs.readFile(filePath)).toString();
const fileName = path.basename(filePath, path.extname(filePath));
const htmlPath = path.join(
config ? config.output : argv.output,
configData ? configData.output : argv.output,
fileName + ".html"
);
const html = generateFromText(
text,
fileName,
config ? config.stylesheet : argv.stylesheet
configData ? configData.stylesheet : argv.stylesheet
);
await fs.writeFile(htmlPath, html);
} else if (fileStat.isFile() && path.extname(filePath) == ".md") {
const text = (await fs.readFile(filePath)).toString();
const fileName = path.basename(filePath, path.extname(filePath));
const htmlPath = path.join(
config ? config.output : argv.output,
configData ? configData.output : argv.output,
fileName + ".html"
);
const html = generateFromMd(
text,
fileName,
config ? config.stylesheet : argv.stylesheet
configData ? configData.stylesheet : argv.stylesheet
);
await fs.writeFile(htmlPath, html);
} else {
Expand Down

0 comments on commit 0173054

Please sign in to comment.