import { format } from 'date-fns' ; import { readFileSync } from 'fs' ; import { basename , relative } from 'path' ; import Handlebars from 'handlebars' ; import { EnhancedOpts } from './domain' ; const footerFragment = readFileSync ( './src/templates/footer_fragment.handlebars' ) ; const generateFooterFrom = ( options ) => { const context = { SNAPSHOT_DATE : format ( options . snapshotDate , 'yyyy-MM' ) , ARTICLE_URL : `https://${ options . hostingDNSDomain }/wiki/${ encodeURIComponent ( options . relativeFilepath ) }` , ARTICLE_URL_DISPLAY : `https://${ options . hostingDNSDomain }/wiki/${ options . relativeFilepath }` , IPNS_HASH : options . hostingIPNSHash , CANONICAL_URL : options . canonicalUrl , CANONICAL_URL_DISPLAY : decodeURIComponent ( options . canonicalUrl ) , IMAGES_DIR : options . relativeImagePath , ZIM_NAME : basename ( options . zimFile ) } ; const footerTemplate = Handlebars . compile ( footerFragment . toString ( ) ) ; const footer = footerTemplate ( context ) ; return footer ; } ; export const appendFooter = ( $html , options ) => { const footer = generateFooterFrom ( options ) ; $html ( '#distribution-footer' ) . remove ( ) ; $html ( '#mw-mf-page-center' ) . append ( `<div id='distribution-footer'>${ footer }</div>` ) ; } ; export const appendHtmlPostfix = ( href ) => { return href ; } ; export const prefixRelativeRoot = ( href ) => { if ( ! href . startsWith ( '/wiki/' ) ) { return href ; } return href . replace ( '/wiki/' , './' ) ; } ; export const moveRelativeLinksUpOneLevel = ( href ) => { return href . replace ( '../' , '' ) ; } ; export const moveRelativeLinksDownOneLevel = ( href ) => { if ( ! ( href . startsWith ( '../' ) || href . startsWith ( 'http' ) || href . startsWith ( '//' ) ) ) { return `../${ href }` ; } return href . replace ( '../' , '../../' ) ; } ; export const makeScriptLinksRelativeToWiki = ( href ) => { if ( ! href . startsWith ( '-/' ) ) { return href ; } return `../${ href }` ; } ; export const replaceANamespaceWithWiki = ( href ) => { return href . replace ( '/A/' , '/wiki/' ) ; } ; export const reworkLinks = ( $html , selector = 'a:not(.external)' , fns = [ replaceANamespaceWithWiki , appendHtmlPostfix ] ) => { const links = $html ( selector ) ; for ( const link of Object . values < any > ( links ) ) { const attribs = link . attribs ; if ( ! attribs || ! attribs . href ) { continue ; } let href = attribs . href ; for ( const fn of fns ) { href = fn ( href ) ; } attribs . href = href ; } } ; export const reworkScriptSrcs = ( $html , selector = 'a:not(.external)' , fns = [ replaceANamespaceWithWiki , appendHtmlPostfix ] ) => { const scripts = $html ( selector ) ; for ( const script of Object . values < any > ( scripts ) ) { const attribs = script . attribs ; if ( ! attribs || ! attribs . src ) { continue ; } let src = attribs . src ; for ( const fn of fns ) { src = fn ( src ) ; } attribs . src = src ; } } ; export const reworkRedirect = ( $html , selector = 'meta[http-equiv="refresh"]' , fns = [ moveRelativeLinksDownOneLevel ] ) => { const redirects = $html ( selector ) ; for ( const redirect of Object . values < any > ( redirects ) ) { const attribs = redirect . attribs ; if ( ! attribs || ! attribs . content ) { continue ; } let { content } = attribs ; let [ delay , url ] = content . split ( ';url=' ) ; for ( const fn of fns ) { url = fn ( url ) ; } attribs . content = `${ delay };url=${ url }` ; } } ;