Skip to content

Instantly share code, notes, and snippets.

@almaron
Last active May 23, 2019 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almaron/62b2ed931f70f4acbf655be8f27f0b47 to your computer and use it in GitHub Desktop.
Save almaron/62b2ed931f70f4acbf655be8f27f0b47 to your computer and use it in GitHub Desktop.
BBcode Parser
# line breaks are normal \n
Ok, here we go with the [b]sample[/b] that needs to be [b][i]parsed[/i][/b] into proper JSON format.
We can have multiple lines inside a paragraph, with [url=http://example.com]links[/url] to different [b][url=http://manager.example.com]websites[/url][/b].
This is another paragraph with an inline image. [img]http://via.placeholder.com/280x140/[/img]
[img]http://via.placeholder.com/200x140/[/img] can be in the beginning of the parapraph. It can also be inside of a link:
[url=http://image.example.com][img]http://via.placeholder.com/280x100/[/img][/url] so it's tricky.
[list]
[*] here is an unordered list
[*] with [b]various[/b] line items
[*] can be a link [url=http://example.com]here as well[/url]
[/list]
[align=center] This is a centered paragraph. Nothing interesting here. [/align]
[
{
"object":"block",
"type":"paragraph",
"data":{
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"Ok, here we go with the ",
"marks":[
]
},
{
"object":"leaf",
"text":"sample",
"marks":[
{
"object":"mark",
"type":"bold",
"data":{
}
}
]
},
{
"object":"leaf",
"text":" that needs to be ",
"marks":[
]
},
{
"object":"leaf",
"text":"parsed",
"marks":[
{
"object":"mark",
"type":"underlined",
"data":{
}
},
{
"object":"mark",
"type":"italic",
"data":{
}
},
{
"object":"mark",
"type":"bold",
"data":{
}
}
]
},
{
"object":"leaf",
"text":" into proper JSON format.\nWe can have multiple lines inside a paragraph, with ",
"marks":[
]
}
]
},
{
"object":"inline",
"type":"link",
"data":{
"href":"http://example.com"
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"links",
"marks":[
]
}
]
}
]
},
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":" to different ",
"marks":[
]
}
]
},
{
"object":"inline",
"type":"link",
"data":{
"href":"http://manager.example.com"
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"websites",
"marks":[
{
"object":"mark",
"type":"bold",
"data":{
}
}
]
}
]
}
]
},
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":".",
"marks":[
]
}
]
}
]
},
{
"object":"block",
"type":"paragraph",
"data":{
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"This is another paragraph with an inline image. ",
"marks":[
]
}
]
},
{
"object":"inline",
"type":"image",
"data":{
"src":"http://via.placeholder.com/280x140/"
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"",
"marks":[
]
}
]
}
]
},
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"",
"marks":[
]
}
]
}
]
},
{
"object":"block",
"type":"paragraph",
"data":{
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"",
"marks":[
]
}
]
},
{
"object":"inline",
"type":"image",
"data":{
"src":"http://via.placeholder.com/200x140/"
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"",
"marks":[
]
}
]
}
]
},
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":" can be in the beginning of the parapraph. It can also be inside of a link:\n",
"marks":[
]
}
]
},
{
"object":"inline",
"type":"link",
"data":{
"href":"http://image.example.com"
},
"nodes":[
{
"object":"inline",
"type":"image",
"data":{
"src":"http://via.placeholder.com/280x100/"
}
}
]
},
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":" so it's tricky.",
"marks":[
]
}
]
}
]
},
{
"object":"block",
"type":"bulleted-list",
"data":{
},
"nodes":[
{
"object":"block",
"type":"list-item",
"data":{
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"here is an unordered list",
"marks":[
]
}
]
}
]
},
{
"object":"block",
"type":"list-item",
"data":{
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"with ",
"marks":[
]
},
{
"object":"leaf",
"text":"various",
"marks":[
{
"object":"mark",
"type":"bold",
"data":{
}
}
]
},
{
"object":"leaf",
"text":" line items",
"marks":[
]
}
]
}
]
},
{
"object":"block",
"type":"list-item",
"data":{
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"can be a link",
"marks":[
]
}
]
},
{
"object":"inline",
"type":"link",
"data":{
"href":"http://example.com"
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":" here as well",
"marks":[
]
}
]
}
]
},
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"",
"marks":[
]
}
]
}
]
}
]
},
{
"object":"block",
"type":"paragraph",
"data":{
"align":"center"
},
"nodes":[
{
"object":"text",
"leaves":[
{
"object":"leaf",
"text":"This is a centered paragraph. Nothing interesting here. ",
"marks":[
]
}
]
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment