npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

insert-filecontent-into-markdown

v2.0.1

Published

A markdown postprocessor for easy code example management.

Downloads

11

Readme

insert-filecontent-into-markdown

This package is a markdown postprocessor which is meant to help with creating and maintain markdown files which contain the content other files as code blocks.

usage

You just write your markdown and put placeholder where you want to put other files content.

The overwrite / create behavior is driven by a internal ruleset which is based on the input filenames. If your file is named template.*.md this tool will create a corresponding *.md which is handy if you only want to include the *.template.md file into your code repository. If you list a file which does not fit this pattern it will replace this file with the generated version.

The full syntax is

 <!-- USEFILE: <filename>[; <custom js preprocessor>] -->
```<languageTag>
```

Remove the leading space before the <!--.

The preprocessor, codeblock and language tag are optional, if you provide no language tag, the file extension will be use.

example

This is an example of the usage of this package with the *.template.md naming.

# This is an example file


<!-- USEFILE: test.txt -->

This is some other text.

This tool will infer the type of the inserted code from the file ending, if you explicitly add a code block it will use the given type.

<!-- USEFILE: test.txt -->```js```
<!-- USEFILE: test.txt; str => str.replace('text', 'other text') -->

You can find this in example\template.example.md

Which results in

# This is an example file


<!-- USEFILE: test.txt -->
``` txt
This is some text!
```
*You can find this in `test.txt`*

This is some other text.

This tool will infer the type of the inserted code from the file ending, if you explicitly add a code block it will use the given type.

<!-- USEFILE: test.txt -->``` js
This is some text!
```
*You can find this in `test.txt`*

<!-- USEFILE: test.txt; str => str.replace('text', 'other text') -->``` txt
This is some other text!
```
*You can find this in `test.txt`*

You can find this in example\example.md

Actually this README uses the template alternative.

preprocessor

You can provide custom processors which will be called on each code block inserted. There are three ways to define those.

  1. One is to attach them to the comment block, like so: <!-- USEFILE: ./some-path.js; str => str.slice(0, 20)--> (have a look at the README.template.md where this is also used).
  2. Another is to provided it with the --preprocessor CLI option (see below).
  3. The last one is to provide a file path with the --preprocessorPath CLI option (see below).

They will be called in this order. In each case the function has the following signature:

interface Processor {
  (
    snippet: string,
    inputPath: string,
    outputPath: string,
    replacePath: string
  ): string;
}

You can find this in index.ts

CLI

This CLI is exposed in with two command names insert-filecontent-into-markdown and ifim as a shortcut.

This package provides the following command line options:

insert-filecontent-into-markdown file-a.md

This command will lookup file-a.md and process it according to the rules stated above.

insert-filecontent-into-markdown --add-location-notice file-a.md

This will insert a notice (like the one you can see above) at the bottom of the inserted codeblocks. You should add an -- before your files if you have any files beginning with - or --.

insert-filecontent-into-markdown --preprocess "str => str.replace('str', 'another str')"

You can provide a custom preprocessor which will be called for each string which is inserted into the file.

You can provide this option multiple time, in which case each processor will be called in the specified order.

insert-filecontent-into-markdown --preprocessPath "./my-preproc-file"

You can also put your preprocessor into a file. This file will be require() and the returned value will be used as processor you you should default export your function like so module.exports = str => str.replace('str', 'another str').

You can provide this option multiple time, in which case each processor will be called in the specified order but after the specified --preprocess preprocessor.

insert-filecontent-into-markdown README.md --add-location-notice --paths '[["test.x","test.y"]]'

This allows you to define additional file inputs with their file output. The value passed for paths has to be a two dimensional array, in which you provide arrays with the length of two, where the first element is the source file and the second is the destination file.

using this to insert markdown into markdown

Ironically this package has a caveat for markdown insertions. Use this with caution if your inserted markdown contains code blocks (```) and you are using the replacing approach.