# marked-code-format

> A marked extension for formatting code blocks using Prettier

Latest version **1.1.6** (published 2024-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install marked-code-format
pnpm add marked-code-format
yarn add marked-code-format
bun add marked-code-format
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.6 |
| Published | 2024-08-29 |
| First published | 2023-09-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 17.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 61 |
| Author | Beni Arisandi |
| Maintainers | bent10 |
| Keywords | marked, marked-extension, format, prettier, beautify, code, codefence, stilearning-marked-extensions |

## Links

- npm: https://www.npmjs.com/package/marked-code-format
- Repository: https://github.com/bent10/marked-extensions
- Homepage: https://github.com/bent10/marked-extensions/tree/main/packages/code-format
- Issues: https://github.com/bent10/marked-extensions/issues
- npm.io page: https://npm.io/package/marked-code-format

## Dependencies (1)

- [attributes-parser](https://npm.io/package/attributes-parser.md) ^2.2.3

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 1.1.6 (latest) — 2024-08-29
- 1.1.5 — 2024-05-16
- 1.1.4 — 2024-02-03
- 1.1.3 — 2023-12-16
- 1.1.2 — 2023-10-26
- 1.1.1 — 2023-10-26
- 1.1.0 — 2023-10-20
- 1.0.1 — 2023-10-14
- 1.0.0 — 2023-09-26

## README

# marked-code-format

A [marked](https://marked.js.org/) extension for formatting code blocks using [Prettier](https://prettier.io/).

## Install

You can install `marked-code-format` using npm or yarn:

```bash
npm i marked-code-format
# or
yarn add marked-code-format
```

**Note:** Be sure to install the `prettier` package as well.

## Usage

Once you've installed this extension, you can use it in your marked configuration. Here's an example of how to configure it:

### Browser

Say we have the following file `example.html`:

```html
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Marked code format extension</title>
  </head>
  <body>
    <div id="content"></div>

    <script src="https://unpkg.com/prettier@3.0.3/standalone.js"></script>
    <script src="https://unpkg.com/prettier@3.0.3/plugins/graphql.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/moo/moo.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/json-loose/dist/index.umd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/attributes-parser/dist/index.umd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked-code-format/dist/index.umd.min.js"></script>
    <script>
      ;(async () => {
        const md = `# Example

\`\`\`graphql prettier
query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) { name friends @include(if: $withFriends) { name } }
}
\`\`\`
`

        document.getElementById('content').innerHTML = await new marked.Marked({
          async: true
        })
          .use(markedCodeFormat({ plugins: prettierPlugins }))
          .parse(md)
      })()
    </script>
  </body>
</html>
```

[![Try marked-code-format on RunKit](https://badge.runkitcdn.com/example.html.svg)](https://untitled-x1qoy3y2fnke.runkit.sh/)

### Node.js

Say we have the following file `example.md`:

````md
# Example

```graphql prettier
query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
    }
  }
}
```
````

**🚨 Important:** The `prettier` attribute must be specified in code fence blocks for formatting the code.

And our module `example.js` looks as follows:

```js
import { readFileSync } from 'node:fs'
import { Marked } from 'marked'
import markedCodeFormat from 'marked-code-format'

const html = new Marked()
  .use(
    markedCodeFormat({
      /* Prettier options */
    })
  )
  .parse(readFileSync('example.md', 'utf8'))

console.log(html)
```

Now, running node `example.js` yields:

````html
<h1>Example</h1>

```html
<div>
  <p>Greetings, traveler! Sign up today!</p>
</div>
```
````

## Inline Options

Prettier configuration can be specified in code fence blocks using the `prettier` attribute, it has a higher priority than extension `Options`. For example:

````md
```ts prettier="{ parser: 'typescript' }"
// your code here
```
````

## Related

- [marked-code-jsx-renderer](https://github.com/bent10/marked-extensions/tree/main/packages/code-jsx-renderer)
- [marked-code-preview](https://github.com/bent10/marked-extensions/tree/main/packages/code-preview)

## Contributing

We 💛&nbsp; issues.

When committing, please conform to [the semantic-release commit standards](https://www.conventionalcommits.org/). Please install `commitizen` and the adapter globally, if you have not already.

```bash
npm i -g commitizen cz-conventional-changelog
```

Now you can use `git cz` or just `cz` instead of `git commit` when committing. You can also use `git-cz`, which is an alias for `cz`.

```bash
git add . && git cz
```

## License

![GitHub](https://img.shields.io/github/license/bent10/marked-extensions)

A project by [Stilearning](https://stilearning.com) &copy; 2023-2024.

---
_Source: https://npm.io/package/marked-code-format · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
