0.2.1 • Published 4 years ago

typed-bbcode v0.2.1

Weekly downloads
3
License
BSD-2-Clause
Repository
github
Last release
4 years ago

typed-bbcode

Convert BBCode (bulletin board code) into HTML

npm Package Version

Supported BBCode

  • img
  • size
  • font
  • color
  • i
  • b
  • url
  • attach

Installation

npm i --save typed-bbcode

Usage

Extract image links from bbcode

import { extract_images_from_bbcode } from 'typed-bbcode'

const text = `
some text [img]http://host.net/image-1.jpg[/img] [img]http://host.net/image-2.jpg[/img]
more text
[img]http://host.net/image-3.jpg[/img]
and more
`
const imgs = extract_images_from_bbcode(text)
/*
[
  'http://host.net/image-1.jpg',
  'http://host.net/image-2.jpg',
  'http://host.net/image-3.jpg',
]
*/

Conversion bbcode to html

Without attachments

import fs from 'fs'
let input = fs.readFileSync('test/in.html').toString() // load string input

import { bbcode_to_html } from 'typed-bbcode'
let output = bbcode_to_html(input) // output is string
fs.writeFileSync('test/out.html', output)

With attachments

import { bbcode_to_html } from 'typed-bbcode'

let code = `
some desc
[attach]1[/attach]

some more desc
[attach]2[/attach]
`

let attachments = [
  '/attachments/202004/17/image1.jpg',
  '/attachments/202004/17/image2.jpg',
]

// optionally supply the attachment list
let output = bbcode_to_html(code, {
  attachment: {
    links: attachments,
    mode: 'img',
  },
})
/*
some desc
<img src="/attachments/202004/17/image1.jpg">

some more desc
<img src="/attachments/202004/17/image2.jpg">
*/