notebook | nice name | Cell 15 | Search

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.

Run example

npm run import -- "authoring header template"

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;

What the code could have been:

/**
 * 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:

  1. Function Definition:

  2. Header Generation:

  3. Return Value:

  4. Module Export:

In essence, this code provides a reusable way to generate a standardized header comment for code, incorporating copyright information and potentially additional markdown content.