import { PackageChangelog } from '../types/ChangeLog' ; import { renderPackageChangelog , defaultRenderers } from './renderPackageChangelog' ; import { ChangelogOptions , PackageChangelogRenderInfo } from '../types/ChangelogOptions' ; export interface MarkdownChangelogRenderOptions extends Omit < PackageChangelogRenderInfo , 'renderers' > { previousContent : string ; changelogOptions : ChangelogOptions ; } export const markerComment = '<!-- Start content -->' ; export async function renderChangelog ( renderOptions ) { const { previousJson , previousContent = '' , newVersionChangelog , isGrouped , changelogOptions : { renderPackageChangelog : customRenderPackageChangelog , customRenderers } , } = renderOptions ; let previousLogEntries ; if ( previousContent . includes ( markerComment ) ) { previousLogEntries = previousContent . split ( markerComment , 2 ) [ 1 ] . trim ( ) ; } else { const h2Match = previousContent . match ( /^## /m ) ; previousLogEntries = h2Match ? previousContent . substring ( h2Match . index ! ) : previousContent ; } try { if ( customRenderPackageChangelog || customRenderers ) { console . log ( 'Using custom renderer for package version changelog.' ) ; } const renderInfo = { previousJson , newVersionChangelog , isGrouped , renderers : { ... defaultRenderers , ... customRenderers , } , } ; return ( [ renderChangelogHeader ( newVersionChangelog ) , markerComment , await ( customRenderPackageChangelog || renderPackageChangelog ) ( renderInfo ) , previousLogEntries , ] . join ( '\n\n' ) . trim ( ) + '\n' ) ; } catch ( err ) { console . log ( 'Error occurred rendering package version changelog:' , err ) ; return '' ; } } function renderChangelogHeader ( changelog ) { return ( `# Change Log - ${ changelog . name }\n\n` + `This log was last generated on ${ changelog . date . toUTCString ( ) } and should not be manually modified.` ) ; }