npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

line-markdown

v0.1.8

Published

line-markdown is a converter that transforms Markdown into Flex Message for the LINE Messaging API.

Downloads

9

Readme

line-markdown

line-markdown is a converter that transforms Markdown into Flex Message for the LINE Messaging API.

Installation

npm install line-markdown --save

Usage

Basic usage

Convert the markdown to a Flex Message.

Code

import { convertToFlexMessage } from 'line-markdown'
import * as line from '@line/bot-sdk'

const markdownText = `
# Fruits
* apple
* banana
* cherry
`.trim()

convertToFlexMessage(markdownText)
  .then(({ flexMessage }) => {
    const client = new line.messagingApi.MessagingApiClient({
      channelAccessToken: '{{YOUR_CHANNEL_ACCESS_TOKEN}}'
    })
    return client.pushMessage({
      to: '{{YOUR_USER_ID}}',
      messages: [flexMessage]
    })
  })
  .then(() => {
    console.log('sent.')
  })

Result

Talk List Screen

The default alternative text is markdown, so the talk list screen will display markdown.

Example1 Alt

Talk Screen

The default size of the Flex message bubble is giga.

Example1 Flex

Custom Alternative text

To set custom alternative text and set the bubble size to mega, use the following code.

Code

import { convertToFlexMessage } from 'line-markdown'
import * as line from '@line/bot-sdk'

const markdownText = `
# Fruits
* apple
* banana
* cherry
`.trim()

convertToFlexMessage(markdownText, 'Fruits', { size: 'mega' })
  .then(({ flexMessage }) => {
    const client = new line.messagingApi.MessagingApiClient({
      channelAccessToken: '{{YOUR_CHANNEL_ACCESS_TOKEN}}'
    })
    return client.pushMessage({
      to: '{{YOUR_USER_ID}}',
      messages: [flexMessage]
    })
  })

Result

Talk List Screen

The alternative text is Fruits.

Example2 Alt

Talk Screen

The size of the Flex message bubble is set to mega.

Example2 Flex

Code Display

line-markdown also supports the display of code.

Code

import { convertToFlexMessage } from 'line-markdown'
import * as line from '@line/bot-sdk'

const markdownText =
  [
    '```typescript                                 ',
    'const add = (a:number, b:number): number => { ',
    '  return a + b                                ',
    '}                                             ',
    '```                                           '
  ].join("\n")

convertToFlexMessage(markdownText, 'Typescript sample')
  .then(({ flexMessage, textType }) => {
    console.log(textType) // => "code"
    const client = new line.messagingApi.MessagingApiClient({
      channelAccessToken: '{{YOUR_CHANNEL_ACCESS_TOKEN}}'
    })
    return client.pushMessage({
      to: '{{YOUR_USER_ID}}',
      messages: [flexMessage]
    })
  })

Result

Talk List Screen

Example3 Alt

Talk Screen

Example3 Flex

Flex Bubble

You can convert Markdown into a Flex Bubble, which allows you to use Markdown as part of a carousel.

Code

import { convertToFlexBubble } from 'line-markdown'
import * as line from '@line/bot-sdk'

const markdownText = `
# Fruits
* apple
* banana
* cherry
`.trim()
convertToFlexBubble(markdownText, { size: 'micro' })
  .then(({ flexBubble }) => {
    const message = {
      type: "flex",
      altText: 'Fruits',
      contents: {
        type: 'carousel',
        contents: [
          flexBubble,
          {
            type: "bubble",
            size: "micro",
            body: {
              type: "box",
              layout: "vertical",
              justifyContent: "center",
              contents: [
                {
                  type: "button",
                  action: {
                    type: "uri",
                    label: "Show more",
                    uri: "http://linecorp.com/"
                  }
                }
              ]
            }
          }
        ]
      }
    }
    const client = new line.messagingApi.MessagingApiClient({
      channelAccessToken: '{{YOUR_CHANNEL_ACCESS_TOKEN}}'
    })
    return client.pushMessage({
      to: '{{YOUR_USER_ID}}',
      messages: [message]
    })
  })

Result

Talk List Screen

Example4 Alt

Talk Screen

Markdown is applied as part of the carousel.

Example4 Flex

Flex Box

You can convert Markdown into a Flex Box. This allows you to use Markdown as part of a Flex Bubble.

Code

import { convertToFlexBox } from 'line-markdown'
import * as line from '@line/bot-sdk'

const markdownText = `
# Fruits
* apple
* banana
* cherry
`.trim()
convertToFlexBox(markdownText)
  .then(({ flexBox }) => {
    const message = {
      type: "flex",
      altText: 'Fruits',
      contents: {
        type: "bubble",
        size: 'mega',
        body: {
          type: "box",
          layout: "vertical",
          contents: [
            flexBox,
            {
              type: "button",
              action: {
                type: "uri",
                label: "Show more",
                uri: "http://linecorp.com/"
              }
            }
          ]
        }
      }
    }
    const client = new line.messagingApi.MessagingApiClient({
      channelAccessToken: '{{YOUR_CHANNEL_ACCESS_TOKEN}}'
    })
    return client.pushMessage({
      to: '{{YOUR_USER_ID}}',
      messages: [message]
    })
  })

Result

Talk List Screen

Example5 Alt

Talk Screen

A button is positioned below the Markdown.

Example5 Flex

Methods

convertToFlexMessage(markdown, altText?, options?)

Convert the markdown to a Flex Message.

  • Parameters

    • markdown
      • The Markdown content you want to convert.
    • altText (Optional)
      • Alternative text. The default value is markdown.
    • options.size (Optional)
      • The size of the Flex Bubble. Acceptable values are nano, micro, deca, hecto, kilo, mega, giga. The default value is giga.
  • Return Value

    • flexMessage
      • The object of the Flex Message.
    • textType
      • The type of text in the inputted markdown. Usually returns markdown. If it contains only code, it returns code, and for content without markdown elements, it returns plain.
convertToFlexMessage(
  markdown: string,
  altText: string = 'markdown',
  options: ConvertOptions = {}
): Promise<{flexMessage: FlexMessage, textType: TextType}>

convertToFlexBubble(markdown, options?)

Convert the markdown to a Flex Message.

  • Parameters

    • markdown
      • The Markdown content you want to convert.
    • options.size (Optional)
      • The size of the Flex Bubble. Acceptable values are nano, micro, deca, hecto, kilo, mega, giga. The default value is giga.
  • Return Value

    • flexBubble
      • The object of the Flex Bubble.
    • textType
      • The type of text in the inputted markdown. Usually returns markdown. If it contains only code, it returns code, and for content without markdown elements, it returns plain.
convertToFlexBubble(markdown: string, options: ConvertOptions = {}):
   Promise<{flexBubble: FlexBubble, textType: TextType}>

convertToFlexBox(markdown)

Convert the markdown to a Flex Message.

  • Parameters

    • markdown
      • The Markdown content you want to convert.
  • Return Value

    • flexBox
      • The object of the Flex Box.
    • textType
      • The type of text in the inputted markdown. Usually returns markdown. If it contains only code, it returns code, and for content without markdown elements, it returns plain.
convertToFlexBox(markdown: string): Promise<{ flexBox: FlexBox, textType: TextType }>

License

MIT