The authorTemplate
function creates a formatted header comment for code, including copyright details and markdown content, which can be easily reused in other code files.
npm run import -- "authoring header template"
// TODO: move this heading authoring template to utility function
function authorTemplate(markdown, code) {
return `
/**
* Written by Brian Cullinan, exported using magic.
* Copyright (c) ${(new Date()).getFullYear()} by Brian Cullinan, All rights reserved.
*
${((markdown || '') + '').split('\n').map(l => ' * ' + l).join('\n')}
*
**/
${code}`;
}
module.exports = authorTemplate;
/**
* Heading authoring template utility function.
*
* Creates a template string with the specified Markdown and code content.
*
* @param {string} markdown - The Markdown content to include in the template.
* @param {string} code - The code content to include in the template.
* @returns {string} The generated template string.
*/
function authorTemplate(markdown, code) {
// Extract the current year for the copyright notice
const currentYear = new Date().getFullYear();
// Define the template variables
const templateVariables = {
author: 'Brian Cullinan',
copyrightYear: currentYear,
};
// Use template literals to generate the template string
return `
/**
* Written by ${templateVariables.author}, exported using magic.
* Copyright (c) ${templateVariables.copyrightYear} by ${templateVariables.author}, All rights reserved.
*
${(markdown || '').split('\n').map(line =>'*'+ line.trim()).join('\n')}
*
**/
${code}`;
}
module.exports = authorTemplate;
This code defines a function called authorTemplate
that generates a header comment block for code, including copyright information and a list of markdown lines.
Here's a breakdown:
Function Definition:
function authorTemplate(markdown, code)
: Defines a function named authorTemplate
that takes two arguments:
markdown
: A string containing markdown text.code
: A string containing the code itself.Header Generation:
markdown
argument, each prefixed with " * ".code
argument is appended to the end of the header.Return Value:
Module Export:
module.exports = authorTemplate;
: Makes the authorTemplate
function available for use in other modules.In essence, this code provides a reusable way to generate a standardized header comment for code, incorporating copyright information and potentially additional markdown content.