import * as ast from "../ast" ; import * as files from "../filesystem/index" ; import { Target , Types } from "./index" ; import { targetURL } from "./target-url" ; export class List { readonly targets : Record < string , Target [ ] > ; constructor ( ) { this . targets = { } ; } addNodeList ( nodeList ) { for ( const node of nodeList ) { const key = node . location . file . platformified ( ) ; this . targets [ key ] = this . targets [ key ] || [ ] ; if ( node . type === "anchor_open" ) { this . addAnchor ( node ) ; } else if ( /h[1-6]_open/ . test ( node . type ) ) { this . addHeading ( node , nodeList ) ; } } } addAnchor ( node ) { if ( node . attributes . href !== undefined ) { return ; } if ( ! node . attributes . name ) { return ; } this . addLinkTarget ( node . location . file , "anchor" , node . attributes . name ) ; } addHeading ( node , nodeList ) { const content = nodeList . textInNode ( node ) ; if ( ! content ) { return ; } this . addLinkTarget ( node . location . file , "heading" , content ) ; } addLinkTarget ( file , type , name ) { const key = file . platformified ( ) ; this . targets [ key ] = this . targets [ key ] || [ ] ; this . targets [ key ] . push ( { name : targetURL ( name ) , type } ) ; } anchorType ( file , name ) { const anchorsForFile = this . targets [ file . platformified ( ) ] ; if ( ! anchorsForFile ) { throw new Error ( `no anchors in file ${ file . platformified ( ) }` ) ; } const anchor = anchorsForFile . find ( linkTarget => linkTarget . name === name ) ; if ( ! anchor ) { throw new Error ( `no anchor '${ name }' in file '${ file . platformified ( ) }'` ) ; } return anchor . type ; } getAnchor ( file , name ) { for ( const target of this . targets [ file . platformified ( ) ] || [ ] ) { if ( target . name === name ) { return target ; } } return null ; } /** provides all the anchors for the given file */ getAnchors ( file ) { return ( this . targets [ file . platformified ( ) ] || [ ] ) . map ( target => target . name ) ; } hasAnchor ( file , name ) { return this . getAnchor ( file , name ) != null ; } hasFile ( filePath ) { return this . targets [ filePath . platformified ( ) ] != null ; } }