|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Newtonsoft.Json; |
| 5 | + |
| 6 | +namespace React.RenderFunctions |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Render functions for React-Helmet. https://github.com/nfl/react-helmet |
| 10 | + /// Requires `react-helmet` to be exposed globally as `Helmet` |
| 11 | + /// </summary> |
| 12 | + public class ReactHelmetFunctions : RenderFunctionsBase |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Constructor. Supports chained calls to multiple render functions by passing in a set of functions that should be called next. |
| 16 | + /// The functions within the provided RenderFunctions will be called *after* this instance's. |
| 17 | + /// Supports null as an argument. |
| 18 | + /// </summary> |
| 19 | + /// <param name="renderFunctions">The chained render functions to call</param> |
| 20 | + public ReactHelmetFunctions(IRenderFunctions renderFunctions = null) |
| 21 | + : base(renderFunctions) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Dictionary of Helmet properties, rendered as raw HTML tags |
| 27 | + /// Available keys: "base", "bodyAttributes", "htmlAttributes", "link", "meta", "noscript", "script", "style", "title" |
| 28 | + /// </summary> |
| 29 | + public Dictionary<string, string> RenderedHelmet { get; private set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Implementation of PostRender |
| 33 | + /// </summary> |
| 34 | + /// <param name="executeJs"></param> |
| 35 | + protected override void PostRenderCore(Func<string, string> executeJs) |
| 36 | + { |
| 37 | + var helmetString = executeJs(@" |
| 38 | +var helmetResult = Helmet.default.renderStatic(); |
| 39 | +JSON.stringify(['base', 'bodyAttributes', 'htmlAttributes', 'link', 'meta', 'noscript', 'script', 'style', 'title'] |
| 40 | + .reduce((mappedResults, helmetKey) => Object.assign(mappedResults, { [helmetKey]: helmetResult[helmetKey] && helmetResult[helmetKey].toString() }), {}));"); |
| 41 | + |
| 42 | + RenderedHelmet = JsonConvert.DeserializeObject<Dictionary<string, string>>(helmetString); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments