import fs from 'fs'
import path from 'path'
import os from 'os'

const x = fs.readFileSync(path.join(os.tmpdir(), 'gis-doc-raw.xml'), 'utf8')

function strip(chunk) {
  return chunk
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&quot;/g, '"')
    .replace(/&amp;/g, '&')
    .replace(/<w:tab[^>]*\/>/g, '\t')
    .replace(/<\/w:p>/g, '\n')
    .replace(/<[^>]+>/g, '')
    .replace(/\n{3,}/g, '\n\n')
}

const anchors = [
  '部署在GIS服务上',
  'cmmapgl.accessToken',
  '地图密钥',
  '2.3 地图组件',
  '构造一个地图对象',
  'new cmmapgl.Map',
  'cmmapgl.Marker',
  'addSource',
  'addLayer',
  'CMMapDrawControl',
  'DistrictCluster',
  '路线规划'
]

for (const a of anchors) {
  const i = x.indexOf(a)
  if (i < 0) {
    console.log(`\n[MISS] ${a}`)
    continue
  }
  console.log(`\n######## ${a} ########`)
  console.log(strip(x.slice(i, i + 6000)))
}
