Coder Social home page Coder Social logo

tttocklll / markdown-flex-message Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arahabica/markdown-flex-message

0.0 0.0 0.0 496 KB

๐Ÿ’ฌ markdown-flex-message is a converter that transforms Markdown into Flex Message for the LINE Messaging API.

License: MIT License

TypeScript 99.43% HTML 0.36% SCSS 0.21%

markdown-flex-message's Introduction

markdown-flex-message logo

npm version test codecov

markdown-flex-message

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

Installation

npm install markdown-flex-message --save

Usage

Basic usage

Convert the markdown to a Flex Message.

Code

import { convertToFlexMessage } from 'markdown-flex-message'
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

If an alternative text is not specified, it becomes the first 100 characters of the Markdown text

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 'markdown-flex-message'
import * as line from '@line/bot-sdk'

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

convertToFlexMessage(markdownText, { altText: '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

markdown-flex-message also supports the display of code.

Code

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

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

convertToFlexMessage(markdownText, { altText: '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 'markdown-flex-message'
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 'markdown-flex-message'
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, options)

Convert the markdown to a Flex Message.

  • Parameters

    • markdown
      • The Markdown content you want to convert.
    • options.altText (Optional)
      • Alternative text for flex message. If not specified, it becomes the first 100 characters of the Markdown text.
    • 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,
  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

markdown-flex-message's People

Contributors

arahabica avatar tttocklll avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.