{"id":21302771,"url":"https://github.com/team-griffin/shortcode-to-tree","last_synced_at":"2025-03-15T18:44:46.516Z","repository":{"id":32550718,"uuid":"136716523","full_name":"team-griffin/shortcode-to-tree","owner":"team-griffin","description":"Parse shortcode text into a tree structure (json)","archived":false,"fork":false,"pushed_at":"2022-12-07T01:26:03.000Z","size":1006,"stargazers_count":1,"open_issues_count":17,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-23T04:02:46.486Z","etag":null,"topics":["javascript","parsing","shortcode","tree"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/shortcode-to-tree","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/team-griffin.png","metadata":{"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}},"created_at":"2018-06-09T11:15:08.000Z","updated_at":"2020-09-15T01:20:48.000Z","dependencies_parsed_at":"2023-01-14T21:45:26.630Z","dependency_job_id":null,"html_url":"https://github.com/team-griffin/shortcode-to-tree","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team-griffin%2Fshortcode-to-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team-griffin%2Fshortcode-to-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team-griffin%2Fshortcode-to-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/team-griffin%2Fshortcode-to-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/team-griffin","download_url":"https://codeload.github.com/team-griffin/shortcode-to-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243776656,"owners_count":20346353,"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":["javascript","parsing","shortcode","tree"],"created_at":"2024-11-21T15:57:41.565Z","updated_at":"2025-03-15T18:44:46.498Z","avatar_url":"https://github.com/team-griffin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# shortcode-to-tree\n\n[![npm version](https://badge.fury.io/js/shortcode-to-tree.svg)](https://badge.fury.io/js/shortcode-to-tree)\n\nShortcodes are a nice compromise for human editors between the simplicity of markup languages like markdown \nand the complexities of html.\n\nHowever, all shortcode parsers I have found focus purely on the \"how to render this as html\" aspect of it, but wouldn't it be nice \nif you could have it in a tree data structure before rendering so that it can be walked to allow operations like mapping or side-loading data.\n\n## Usage\n\n```sh\nnpm install shortcode-to-tree\nyarn add shortcode-to-tree\n```\n\n### parser\nThis function is the main powerhouse of this library. You just need to call it with the text that you'd like converted.\n\n```js\nimport { parser, createSimpleTag } from 'shortcode-to-tree';\n\nconst input = 'Hello [b]World![/b]';\n\nconst tree = parser(input, {\n  b: createSimpleTag('b'),\n});\n\nconsole.log(JSON.stringify(tree, null, 2));\n\n/*\n{\n  \"type\": \"element\",\n  \"name\": \"root\",\n  \"elements\": [\n    {\n      \"type\": \"text\",\n      \"text\": \"Hello \"\n    },\n    {\n      \"type\": \"element\",\n      \"name\": \"b\",\n      \"elements\": [\n        {\n          \"type\": \"text\",\n          \"text\": \"World!\"\n        }\n      ]\n    }\n  ]\n}\n*/\n```\n\nYou will notice in the output above that there is this magic `root` element. All trees need roots; and the parser automatically wraps your input shortcode for you (unless you provide one).\n\nCustom shortcodes can also be provided. The two helper functions `createSimpleTag` \u0026 `createAttributeTag` will aid you in this.\nTo utlise these in the parser you can provide as hashmap of them to the parser:\n\n```js\nimport { parser, createAttributeTag } from 'shortcode-to-tree';\n\nconst input = 'Hello [foo bar=\"baz\"]World![/foo]';\n\nconst tree = parser(input, {\n  foo: createAttributeTag('foo'),\n});\n\nconsole.log(JSON.stringify(tree, null, 2));\n\n/*\n{\n  \"type\": \"element\",\n  \"name\": \"root\",\n  \"elements\": [\n    {\n      \"type\": \"text\",\n      \"text\": \"Hello \"\n    },\n    {\n      \"type\": \"element\",\n      \"name\": \"foo\",\n      \"attributes\": {\n        \"bar\": \"baz\"\n      },\n      \"elements\": [\n        {\n          \"type\": \"text\",\n          \"text\": \"World!\"\n        }\n      ]\n    }\n  ]\n}\n*/\n```\n\nAbove you will now notice that the attributes for that shortcode have been supplied via the `attributes` key on a given element.\n\n### createSimpleTag\n\nThis shortcode handler is for tags which do not support attributes, such as `[b]` or `[i]`.\n\n```js\nimport { parser, createSimpleTag } from 'shortcode-to-tree';\n\nconst tags = {\n  foo: createSimpleTag('foo'),\n};\n\nconst input = 'Hello [foo bar=\"baz\"]World![/foo]';\n\nconst tree = parser(input, {\n  foo: createAttributeTag('foo'),\n});\n\nconsole.log(JSON.stringify(tree, null, 2));\n\n/*\n{\n  \"type\": \"element\",\n  \"name\": \"root\",\n  \"elements\": [\n    {\n      \"type\": \"text\",\n      \"text\": \"Hello \"\n    },\n    {\n      \"type\": \"element\",\n      \"name\": \"foo\",\n      \"elements\": [\n        {\n          \"type\": \"text\",\n          \"text\": \"World!\"\n        }\n      ]\n    }\n  ]\n}\n*/\n```\n\n### createAttributeTag\n\nAs shown above this is for shortcodes which require attribute support. All attributes are treated as strings.\n\n```js\nimport { parser, createAttributeTag } from 'shortcode-to-tree';\n\nconst tags = {\n  foo: createAttributeTag('foo'),\n};\n\nconst input = 'Hello [foo bar=\"baz\"]World![/foo]';\n\nconst tree = parser(input, {\n  foo: createAttributeTag('foo'),\n});\n\n\nconsole.log(JSON.stringify(tree, null, 2));\n\n/*\n{\n  \"type\": \"element\",\n  \"name\": \"root\",\n  \"elements\": [\n    {\n      \"type\": \"text\",\n      \"text\": \"Hello \"\n    },\n    {\n      \"type\": \"element\",\n      \"name\": \"foo\",\n      \"attributes\": {\n        \"bar\": \"baz\"\n      },\n      \"elements\": [\n        {\n          \"type\": \"text\",\n          \"text\": \"World!\"\n        }\n      ]\n    }\n  ]\n}\n*/\n```\n\n## Default tags\n\nThis package exposes several pre-configured tags. These can be imported and used like so:\n\n```js\nimport { defaultTags, parser } from 'shortcode-to-tree';\n\nconst tree = parser(input, defaultTags);\n\n```\n\nIf you want to add more tags on top of this you can just merge the default tags into your custom tags:\n\n```js\nimport { defaultTags, createSimpleTag, parser } from 'shortcode-to-tree';\n\nconst tags = {\n  ...defaultTags,\n  foo: createSimpleTag('foo'),\n}\n\nconst tree = parser(input, tags);\n\n```\n\nThe default tags are:\n\n* `b` (simple tag)\n* `i` (simple tag)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteam-griffin%2Fshortcode-to-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteam-griffin%2Fshortcode-to-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteam-griffin%2Fshortcode-to-tree/lists"}