{"id":13716734,"url":"https://github.com/syntax-tree/mdast-zone","last_synced_at":"2025-12-12T04:15:54.512Z","repository":{"id":30434851,"uuid":"33987920","full_name":"syntax-tree/mdast-zone","owner":"syntax-tree","description":"utility to treat HTML comments as ranges or markers in mdast","archived":false,"fork":false,"pushed_at":"2024-09-05T10:46:43.000Z","size":231,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-09T02:23:35.191Z","etag":null,"topics":["mdast","mdast-util","syntax-tree","unist","util","zone"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-04-15T10:37:46.000Z","updated_at":"2025-03-29T22:56:27.000Z","dependencies_parsed_at":"2023-12-04T16:59:28.034Z","dependency_job_id":null,"html_url":"https://github.com/syntax-tree/mdast-zone","commit_stats":{"total_commits":148,"total_committers":3,"mean_commits":"49.333333333333336","dds":"0.027027027027026973","last_synced_commit":"a1e0b72e5ded16aa922721bf5bb6ae617348032b"},"previous_names":["wooorm/mdast-zone"],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fmdast-zone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fmdast-zone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fmdast-zone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fmdast-zone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/mdast-zone/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252826669,"owners_count":21810162,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mdast","mdast-util","syntax-tree","unist","util","zone"],"created_at":"2024-08-03T00:01:13.803Z","updated_at":"2025-12-12T04:15:47.466Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# mdast-zone\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[mdast][] utility to find two comments and replace the content in them.\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`zone(tree, name, handler)`](#zonetree-name-handler)\n  * [`Handler`](#handler)\n  * [`Info`](#info)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Security](#security)\n* [Related](#related)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that lets you find certain comments, then takes the\ncontent between them, and calls a given handler with the result, so that you can\nchange or replace things.\n\n## When should I use this?\n\nThis utility is typically useful when you have certain sections that can be\ngenerated.\nComments are a hidden part of markdown, so they can be used as processing\ninstructions.\nYou can use those comments to define what content to change or replace.\n\nA similar package, [`mdast-util-heading-range`][mdast-util-heading-range], does\nthe same but uses a heading to mark the start and end of sections.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install mdast-zone\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {zone} from 'https://esm.sh/mdast-zone@6'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {zone} from 'https://esm.sh/mdast-zone@6?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay we have the following file, `example.md`:\n\n```markdown\n\u003c!--foo start--\u003e\n\nFoo\n\n\u003c!--foo end--\u003e\n```\n\n…and a module `example.js`:\n\n```js\n/**\n * @import {Plugin} from 'unified'\n * @import {Root} from 'mdast'\n */\n\nimport {zone} from 'mdast-zone'\nimport {remark} from 'remark'\nimport {read} from 'to-vfile'\n\nconst file = await remark()\n  .use(myPluginThatReplacesFoo)\n  .process(await read('example.md'))\n\nconsole.log(String(file))\n\n/** @type {Plugin\u003c[], Root\u003e} */\nfunction myPluginThatReplacesFoo() {\n  return function (tree) {\n    zone(tree, 'foo', function (start, nodes, end) {\n      return [\n        start,\n        {type: 'paragraph', children: [{type: 'text', value: 'Bar.'}]},\n        end\n      ]\n    })\n  }\n}\n```\n\n…now running `node example.js` yields:\n\n```markdown\n\u003c!--foo start--\u003e\n\nBar.\n\n\u003c!--foo end--\u003e\n```\n\n## API\n\nThis package exports the identifier [`zone`][api-zone].\nThere is no default export.\n\n### `zone(tree, name, handler)`\n\nSearch `tree` for a start and end comments matching `name` and change their\n“section” with `handler`.\n\n###### Parameters\n\n* `tree` ([`Node`][node])\n  — tree to change\n* `name` (`string`)\n  — comment name to look for\n* `handler` ([`Handler`][api-handler])\n  — handle a section\n\n###### Returns\n\nNothing (`undefined`).\n\n### `Handler`\n\nCallback called when a section is found (TypeScript type).\n\n###### Parameters\n\n* `start` ([`Node`][node])\n  — start of section\n* `nodes` ([`Array\u003cNode\u003e`][node])\n  — nodes between `start` and `end`\n* `end` ([`Node`][node])\n  — end of section\n* `info` ([`Info`][api-info])\n  — extra info\n\n###### Returns\n\nResults (`Array\u003cNode | null | undefined\u003e`, optional).\n\nIf nothing is returned, nothing will be changed.\nIf an array of nodes (can include `null` and `undefined`) is returned, the\noriginal section will be replaced by those nodes.\n\n### `Info`\n\nExtra info (TypeScript type).\n\n###### Fields\n\n* `parent` ([`Node`][node])\n  — parent of the section\n* `start` (`number`)\n  — index of `start` in `parent`\n* `end` (`number`)\n  — index of `end` in `parent`\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Handler`][api-handler] and\n[`Info`][api-info].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `mdast-zone@^6`,\ncompatible with Node.js 16.\n\n## Security\n\nImproper use of `handler` can open you up to a [cross-site scripting (XSS)][xss]\nattack as the value it returns is injected into the syntax tree.\nThis can become a problem if the tree is later transformed to **[hast][]**.\nThe following example shows how a script is injected that could run when loaded\nin a browser.\n\n```js\nfunction handler(start, nodes, end) {\n  return [start, {type: 'html', value: '\u003cscript\u003ealert(1)\u003c/script\u003e'}, end]\n}\n```\n\nYields:\n\n```markdown\n\u003c!--foo start--\u003e\n\n\u003cscript\u003ealert(1)\u003c/script\u003e\n\n\u003c!--foo end--\u003e\n```\n\nEither do not use user input or use [`hast-util-santize`][hast-util-sanitize].\n\n## Related\n\n* [`mdast-util-heading-range`](https://github.com/syntax-tree/mdast-util-heading-range)\n  — similar but uses headings to mark sections\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/mdast-zone/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/mdast-zone/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-zone.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/mdast-zone\n\n[downloads-badge]: https://img.shields.io/npm/dm/mdast-zone.svg\n\n[downloads]: https://www.npmjs.com/package/mdast-zone\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=mdast-zone\n\n[size]: https://bundlejs.com/?q=mdast-zone\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting\n\n[mdast]: https://github.com/syntax-tree/mdast\n\n[node]: https://github.com/syntax-tree/mdast#nodes\n\n[mdast-util-heading-range]: https://github.com/syntax-tree/mdast-util-heading-range\n\n[hast]: https://github.com/syntax-tree/hast\n\n[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize\n\n[api-zone]: #zonetree-name-handler\n\n[api-handler]: #handler\n\n[api-info]: #info\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["mdast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fmdast-zone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fmdast-zone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fmdast-zone/lists"}