contentcollector: Fix collectContentLineText hook

Before, the hook always ignored the return values provided by the hook
functions. Now the hook functions can change the text by either
returning a string or setting `context.text` to the desired value.

Also drop the `styl` and `cls` context properties. They were never
documented and they were always null.
This commit is contained in:
Richard Hansen 2021-01-22 02:06:06 -05:00 committed by John McLear
parent 4e220538a1
commit e3a47e48f9
2 changed files with 21 additions and 18 deletions

View file

@ -421,7 +421,20 @@ Things in context:
4. text - the text for that line 4. text - the text for that line
This hook allows you to validate/manipulate the text before it's sent to the This hook allows you to validate/manipulate the text before it's sent to the
server side. The return value should be the validated/manipulated text. server side. To change the text, either:
* Set the `text` context property to the desired value and return `undefined`.
* (Deprecated) Return a string. If a hook function changes the `text` context
property, the return value is ignored. If no hook function changes `text` but
multiple hook functions return a string, the first one wins.
Example:
```
exports.collectContentLineText = (hookName, context) => {
context.text = tweakText(context.text);
};
```
## collectContentLineBreak ## collectContentLineBreak

View file

@ -338,24 +338,14 @@ const makeContentCollector = (collectStyles, abrowser, apool, className2Author)
_reachBlockPoint(node, 0, state); _reachBlockPoint(node, 0, state);
if (dom.isTextNode(node)) { if (dom.isTextNode(node)) {
let txt = dom.nodeValue(node);
const tname = dom.getAttribute(node.parentNode, 'name'); const tname = dom.getAttribute(node.parentNode, 'name');
const context = {cc: this, state, tname, node, text: dom.nodeValue(node)};
const txtFromHook = hooks.callAll('collectContentLineText', { // Hook functions may either return a string (deprecated) or modify context.text. If any hook
cc: this, // function modifies context.text then all returned strings are ignored. If no hook functions
state, // modify context.text, the first hook function to return a string wins.
tname, const [hookTxt] =
node, hooks.callAll('collectContentLineText', context).filter((s) => typeof s === 'string');
text: txt, let txt = context.text === dom.nodeValue(node) && hookTxt != null ? hookTxt : context.text;
styl: null,
cls: null,
});
if (typeof (txtFromHook) === 'object') {
txt = dom.nodeValue(node);
} else if (txtFromHook) {
txt = txtFromHook;
}
let rest = ''; let rest = '';
let x = 0; // offset into original text let x = 0; // offset into original text