All files index.ts

54.55% Statements 6/11
100% Branches 2/2
33.33% Functions 1/3
54.55% Lines 6/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x               1x 1x   1x                                                                      
import { ParseError, HtmlParser } from '@starptech/webparser'
import fromWebparser from '@starptech/hast-util-from-webparser'
const VMessage = require('vfile-message')
 
interface ParseOptions {}
interface VFile {
  path: string
  message(msg: any): void
}
 
export = function parse(options?: ParseOptions): any {
  options = options || {}
 
  this.Parser = parser
 
  function parser(doc: string, file: VFile) {
    const parseResult = new HtmlParser({
      ignoreFirstLf: false,
      decodeEntities: false,
      selfClosingCustomElements: true
    }).parse(doc, file.path)
 
    for (const err of parseResult.errors) {
      file.message(createVMessage(err))
    }
 
    return fromWebparser(parseResult.rootNodes, options)
  }
 
  function createVMessage(err: ParseError) {
    return new VMessage(
      err.contextualMessage(),
      {
        start: {
          line: err.span.start.line,
          offset: err.span.start.offset,
          column: err.span.start.col
        },
        end: {
          line: err.span.end.line,
          offset: err.span.end.offset,
          column: err.span.end.col
        }
      },
      'ParseError'
    )
  }
}