Skip to content

Commit

Permalink
fix: handle author link
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Jan 13, 2025
1 parent 2d4b7c8 commit 64c71cf
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/bylines/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ const BYLINE_ID = 'newspack-byline';
const META_KEY_ACTIVE = '_newspack_byline_active';
const META_KEY_BYLINE = '_newspack_byline';

/**
* Get author url given the author id.
*
* @param {number} authorId The author ID.
*
* @return {string} The author URL.
*/
const getAuthorUrl = authorId => {
return `/author/${ authorId }`;
}

/**
* Prepend byline to content in the Editor.
Expand All @@ -35,6 +45,11 @@ const prependBylineToContent = byline => {
bylineEl.id = BYLINE_ID;
contentEl.insertBefore( bylineEl, contentEl.firstChild );
}
// Search for <Author id="123"> tag and replace it with a link to the author page.
byline = byline.replace( /<Author id=(\d+)>/g, ( match, authorId ) => {
return `<a href="${ getAuthorUrl( authorId ) }">`;
} );
byline = byline.replace( /<\/Author>/g, '</a>' );
bylineEl.innerHTML = byline;
}
};
Expand All @@ -56,6 +71,11 @@ const BylinesSettingsPanel = () => {
}
// Byline change handler.
const handleBylineChange = value => {
const tags = value.match( /<[^>]+>/g );
if ( tags && tags.some( tag => ! tag.startsWith( '<Author' ) && ! tag.startsWith( '</Author' ) ) ) {
alert( __( 'Only the <Author> tag is allowed.', 'newspack-plugin' ) ); // eslint-disable-line no-alert
return;
}
editPost( { meta: { [ META_KEY_BYLINE ]: value } } );
setByline( value );
}
Expand Down Expand Up @@ -88,4 +108,3 @@ registerPlugin( 'newspack-bylines-sidebar', {
render: BylinesSettingsPanel,
icon: false,
} );

0 comments on commit 64c71cf

Please sign in to comment.