kgar / ts-markdown

An extensible TypeScript markdown generator that takes JSON and creates a markdown document
https://kgar.github.io/ts-markdown/
MIT License
9 stars 4 forks source link

Utility Entry types #36

Open kgar opened 2 years ago

kgar commented 2 years ago

ts-markdown has a few entries with the append property. While this was a neat idea at first, more ideas are coming to mind about utilities that would be useful, above and beyond append.

Create a series of utility MarkdownEntry types.

Ideas so far:

Reason: There are scenarios where you are unable to reliably perform some quick modifications to an array of markdown entries. For example, certain information is not available at the point where a paragraph is created, and now the paragraph needs to be prepended with some text. Rather than write a bunch of duck typing code to handle every possible scenario, utility entries could build these kinds of post-process needs into markdown rendering via data decoration.

What would the code look like?


// Example: prepend some markdown content to the first item in an array of markdown entries

/*
Let's say the entries are supposed to read:
This is some text.

Nice!
*/
let someEntries = getSomeEntriesFromSomewhere();

let firstEntryWithPrepend = { 
  utilPrepend: { 
    text: [bold("Note:")], entry: someEntries[0]
  }
};

someEntries.splice(0, 1, firstEntryWithPrepend);

/*
Now, it should read:
Note: This is some text.

Nice!
*/

Now, take that idea and create helper functions to help with managing the array mutation / copying:

// Example: prepend some markdown content to the first item in an array of markdown entries

/*
Let's say the entries are supposed to read:
This is some text.

Nice!
*/
let someEntries = getSomeEntriesFromSomewhere();

let someEntriesWithPrepend = prependFirst(someEntries, { text: [bold("Note: ")] });

/*
`someEntriesWithPrepend` should read:
Note: This is some text.

Nice!
*/

This is just a rough sketch of the idea and it will surely evolve after some more thought and experimentation.