Hooks

Hooks allow you to programmatically modify the behavior of the comments embed.

Using Hooks

First, configure the embed to load manually using loading="manual".

<hyvor-talk-comments website-id="1234" loading="manual"></hyvor-talk-comments>

Then, add a script to register hooks and load the embed wrapping it around with customElements.whenDefined method.

<script>
	customElements.whenDefined('hyvor-talk-comments').then(() => {
		const comments = document.querySelector('hyvor-talk-comments');

		// register hooks
		comments.api.hooks.register('time:format', (props) => {
			props.text = props.datetime.toLocaleString();
			return props;
		});

		// load the embed
		comments.load();
	});
</script>

The hooks.register function takes the hook name and a callback. Callback gets an object (props) and should return the same object. Within the callback, you can update the properties of the object to modify the behaviour of the embed. You can find the list of available hooks and the properties of the props object below.

Available Hooks

time:format

Format how time is shown in comments, etc.

Property
Type
Description
datetime
Date
Date object of the time to be shown
text
string
The displayed time (default: "2m ago"). Update this to show a different time

Showing full date:

comments.api.hooks.register('time:format', (props) => {
	props.text = props.datetime.toLocaleString();
	return props;
});

icon

Set custom icons

Property
Type
Description
name
string
Name of the icon
html
string | null
Custom HTML to use instead of the default icon.

Setting a custom reply icon:

comments.api.hooks.register('icon', (props) => {
	if (props.name === 'reply') {
		props.html = '<svg...></svg>';
	}
	return props;
});

Available names: featured, heart, caret, thumbsUp, thumbsDown, thumbsUpActive, thumbsDownActive, link, bold, italic, quote, emoji, image, gif, bell, search, flag, mod, reply

render:comment:before

Add custom HTML to be rendered before the comment

Property
Type
Description
html
string | null
Custom HTML to be rendered
comment
Comment
Comment object
listType
string
Type of the list this comment is part of (main, search, user, or specific)
index
number
Index of the comment within the list, starting from 0

Showing a banner before the first comment of the page:

comments.api.hooks.register('render:comment:before', (props) => {
	if (
		// a parent comment (not a reply)
		props.comment.parent_id === null &&
		// main list
		props.listType === 'main' &&
		// first comment in the list
		props.index === 0
	) {
		props.html = '<banner>My custom banner</banner>';
	}
	return props;
});

render:comment:after

Add custom HTML to be rendered after the comment

Takes the same properties as render:comment:before.

render:comment:before-content

Add custom HTML to be rendered inside the comment, but before the content

Takes the same properties as render:comment:before.

render:comment:after-content

Add custom HTML to be rendered inside the comment, but after the content

Takes the same properties as render:comment:before.

Customize attributes of links in user-generated comments

Property
Type
Description
attrs
Record<string, string | null>
All html attributes of the link element, set to null to remove the attribute.

By default, links in comments have href, target="_blank", and rel="ugc" attributes. You can use this hook to add or remove attributes.

Removing the rel attribute for links that starts with example.org:

comments.api.hooks.register('comment:content:link', (props) => {
	if (props.attrs.href.startsWith('https://example.org')) {
		props.attrs.rel = null;
	}
	return props;
});

t

Dynamically customize a translation using additional context

Property
Type
Description
key
string
The translation key being resolved
text
string
The resolved translation text. Update this to change what is shown
context
Record<string, unknown>
Additional data related to this translation, if any (ex: the comment object). Depends on the key

This hook is called every time a piece of text is translated. It lets you customize a translation dynamically using extra context that is not available to t- attributes, such as comment or user data.

Example of appending user's location data to labels:

comments.api.hooks.register('t', (props) => {
	if (props.key === 'parent_comment_by' || props.key === 'in_reply_to') {
		const user = props.context.user;
		if (user?.location) {
			props.text += ` from ${user.location}`;
		}
	}
	return props;
});
The context object available on props depends on the translation key being resolved. Check the key first before reading properties off it. Context is not documented since there are many possible translation keys - use a simple console.log. Context objects are guaranteed to be backward compatible.

misc:branding

Customize Hyvor Talk branding shown at the end of the comments section

Property
Type
Description
text
string
anchor text
props
string
anchor HTML properties (ex: you can set rel="nofollow")

Common tips

  • Make sure to always return the data object from the callback, even if you don't make any changes to it.
  • Several hooks support custom HTML. To prevent XSS attacks, only set those properties directly or from trusted inputs.