import Immutable from "immutable" ; import location from "../utils/location" ; import File from "./file" ; import Book from "./book" ; import Readme from "./readme" ; type Articles = Immutable . List < SummaryArticle > ; class SummaryArticle extends Immutable . Record ( { level : String ( ) , title : String ( ) , ref : String ( ) , articles : Immutable . List ( ) , } , "SummaryArticle" ) { getLevel ( ) { return this . get ( "level" ) ; } getTitle ( ) { return this . get ( "title" ) ; } getRef ( ) { return this . get ( "ref" ) ; } getArticles ( ) { return this . get ( "articles" ) ; } return { number } getDepth ( ) { return this . getLevel ( ) . split ( "." ) . length - 1 ; } return { string } getPath ( ) { if ( this . isExternal ( ) ) { return undefined ; } const ref = this . getRef ( ) ; if ( ! ref ) { return undefined ; } const parts = ref . split ( "#" ) ; const pathname = parts . length > 1 ? parts . slice ( 0 , - 1 ) . join ( "#" ) : ref ; return location . flatten ( pathname ) ; } return { string } getUrl ( ) { return this . isExternal ( ) ? this . getRef ( ) : undefined ; } return { string } getAnchor ( ) { const ref = this . getRef ( ) ; const parts = ref . split ( "#" ) ; const anchor = parts . length > 1 ? `#${ parts [ parts . length - 1 ] }` : undefined ; return anchor ; } return { string } createChildLevel ( ) { const level = this . getLevel ( ) ; const subArticles = this . getArticles ( ) ; const childLevel = `${ level }.${ subArticles . size + 1 }` ; return childLevel ; } return { boolean } isPage ( ) { return Boolean ( ! this . isExternal ( ) && this . getRef ( ) ) ; } param { File } file return { boolean } isFile ( file ) { return file . getPath ( ) === this . getPath ( ) && this . getAnchor ( ) === undefined ; } param { Book | Readme } book return { boolean } isReadme ( book ) { const readme = book . getFile ? book : book . getReadme ( ) ; const file = readme . getFile ( ) ; return this . isFile ( file ) ; } return { boolean } isExternal ( ) { return location . isExternal ( this . getRef ( ) ) ; } param { Object } def return { SummaryArticle } static create ( def , level ) { const articles = ( def . articles || [ ] ) . map ( ( article , i ) => { if ( article instanceof SummaryArticle ) { return article ; } return SummaryArticle . create ( article , [ level , i + 1 ] . join ( "." ) ) ; } ) ; return new SummaryArticle ( { level : level , title : def . title , ref : def . ref || def . path || "" , articles : Immutable . List ( articles ) , } ) ; } return { boolean } hasAnchor ( ) { const ref = this . getRef ( ) ; return ref . includes ( "#" ) ; } param { Article | Part } base param { Function return { Article } static findArticle ( base , iter ) { const articles = base . getArticles ( ) ; return articles . reduce ( ( result , article ) => { if ( result ) return result ; if ( iter ( article ) ) { return article ; } return SummaryArticle . findArticle ( article , iter ) ; } , null ) ; } } export default SummaryArticle ;