Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es2021": true
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
Expand Down
75 changes: 75 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"assert": "^2.1.0",
"eslint": "^8.52.0",
"eslint-plugin-react": "^7.33.2",
"mocha": "^10.2.0",
Expand Down
94 changes: 55 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const fs = require("fs");
const path = require("path");
const yargs = require("yargs");
const toml = require("toml");
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const toml = require('toml');

// Function to read the content of a file
function readFile(filePath) {
try {
return fs.readFileSync(filePath, "utf8");
return fs.readFileSync(filePath, 'utf8');
} catch (error) {
console.error(`Error reading file: ${error.message}`);
process.exit(1);
Expand Down Expand Up @@ -34,20 +34,30 @@ function processFile(inputFilePath, outputDir, lang) {

console.log(`Converted ${inputFilePath} to HTML.`);
}
module.exports = {
processFile // Ensure processFile is included in the exported object
};

// Function to process all .txt and .md files in a directory
function processDirectory(inputDir, outputDir, lang) {
const files = fs.readdirSync(inputDir);

files.forEach((file) => {
const filePath = path.join(inputDir, file);

if (fs.lstatSync(filePath).isFile() && file.match(/\.(txt|md)$/)) {
processFile(filePath, outputDir, lang);
}
});
}

function generateHTML(fileContent, lang) {
const [title, ...paragraphs] = fileContent.split(/\n{1,}/); // Split by one or more newline characters
const titleHtml = title ? `<title>${title}</title><h1>${title}</h1>` : "";
const titleHtml = title ? `<title>${title}</title><h1>${title}</h1>` : '';
const paragraphsHtml = paragraphs
.map((paragraph) => {
// Support for bold text using double asterisks
paragraph = paragraph.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>");
paragraph = paragraph.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
return `<p>${paragraph}</p>`;
})
.join("\n");
.join('\n');

return `<!doctype html>
<html lang="${lang}">
Expand All @@ -62,7 +72,6 @@ function generateHTML(fileContent, lang) {
</html>`;
}


function processConfigFile(configPath) {
const configContent = readFile(configPath);
return toml.parse(configContent);
Expand All @@ -71,37 +80,37 @@ function processConfigFile(configPath) {
// Main command-line tool logic
function main() {
yargs
.scriptName("til")
.usage("$0 <cmd> [args]")
.scriptName('til')
.usage('$0 <cmd> [args]')
.command(
"process [input]",
"Process .txt and .md file(s) and generate HTML",
'process [input]',
'Process .txt and .md file(s) and generate HTML',
(yargs) => {
yargs
.positional("input", {
describe: "Input .txt file or folder",
type: "string",
.positional('input', {
describe: 'Input .txt file or folder',
type: 'string',
})
.option("output", {
alias: "o",
describe: "Output directory",
type: "string",
.option('output', {
alias: 'o',
describe: 'Output directory',
type: 'string',
})
.option("lang", {
alias: "l",
describe: "language for lang attribute on <html> elements",
type: "string",
default: "en-CA",
.option('lang', {
alias: 'l',
describe: 'language for lang attribute on <html> elements',
type: 'string',
default: 'en-CA',
})
.option("config", {
alias: "c",
describe: "Path to TOML configuration file",
type: "string",
.option('config', {
alias: 'c',
describe: 'Path to TOML configuration file',
type: 'string',
});
},
(argv) => {
const inputPath = argv.input;
const outputDir = argv.output || "til"; // Use 'til' if not specified
let outputDir = argv.output || 'til'; // Use 'til' if not specified
let lang = argv.lang;

// Check if a config file is provided
Expand Down Expand Up @@ -130,17 +139,24 @@ function main() {
processFile(inputPath, outputDir, lang);
} else {
console.error(
"Invalid input. Please provide a valid .txt or .md file or folder."
'Invalid input. Please provide a valid .txt or .md file or folder.'
);
process.exit(1); // Exit with a non-zero exit code
}
}
)
.demandCommand(1, "You need to specify a command.")
.version("v", "Show the tool's name and version", `til v1.0.0`)
.alias("v", "version")
.help("h", "Show a useful help message")
.alias("h", "help").argv;
.demandCommand(1, 'You need to specify a command.')
.version('v', 'Show the tool\'s name and version', `til v1.0.0`)
.alias('v', 'version')
.help('h', 'Show a useful help message')
.alias('h', 'help').argv;
}

main();

// Export the processFile function
module.exports = {
processFile,
processDirectory,
// other exports if any
};
34 changes: 34 additions & 0 deletions test/processDirectory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-env mocha */

const assert = require('assert');
const fs = require('fs');
const { processDirectory } = require('../src/index');

describe('processDirectory Function Test Suite', function () {
const outputDir = './output'; // Set an output directory for testing
const lang = 'en-US'; // Set a language for testing

before(function () {
// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
});

it('Processes all .txt and .md files in a directory', function () {
const inputDir = './src'; // Provide a test directory for input

// Run the function
processDirectory(inputDir, outputDir, lang);

// Verify if the output files were created
const files = fs.readdirSync(outputDir);
const fileExists = files.some(file => file.endsWith('.html'));

// Assertion to check if at least one HTML file exists in the output directory
assert.strictEqual(fileExists, true);
});

// Add more test cases as needed
});

10 changes: 6 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-env mocha */

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { processFile } = require('../src/index');
const { processFile } = require('../src/index.js'); // Adjust the path accordingly

describe('processFile Function Test Suite', function() {
it('Generates HTML and writes to output directory', function() {
describe('processFile Function Test Suite', function () {
it('Generates HTML and writes to output directory', function () {
const inputFilePath = './src/till-1.txt'; // Provide a test file for input
const outputDir = './output'; // Set an output directory for testing
const lang = 'en-US'; // Set a language for testing
Expand All @@ -15,7 +17,7 @@ describe('processFile Function Test Suite', function() {
}

// Run the function
processFile(inputFilePath, outputDir, lang);
processFile(inputFilePath, outputDir, lang); // Use processFile function

// Verify if the output file was created
const expectedOutputFilePath = path.join(outputDir, 'till-1.html');
Expand Down