helpers.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const { version } = require('../../package.json');
  2. module.exports['oz-version'] = () => version;
  3. module.exports['readme-path'] = (opts) => {
  4. return 'contracts/' + opts.data.root.id.replace(/\.adoc$/, '') + '/README.adoc';
  5. };
  6. module.exports.names = (params) => params.map(p => p.name).join(', ');
  7. module.exports['typed-params'] = (params) => {
  8. return params.map(p => `${p.type}${p.name ? ' ' + p.name : ''}`).join(', ');
  9. };
  10. const slug = module.exports.slug = (str) => {
  11. if (str === undefined) {
  12. throw new Error('Missing argument');
  13. }
  14. return str.replace(/\W/g, '-');
  15. };
  16. const linksCache = new WeakMap();
  17. function getAllLinks (items) {
  18. if (linksCache.has(items)) {
  19. return linksCache.get(items);
  20. }
  21. const res = {};
  22. linksCache.set(items, res);
  23. for (const item of items) {
  24. res[`xref-${item.anchor}`] = `xref:${item.__item_context.page}#${item.anchor}`;
  25. res[slug(item.fullName)] = `pass:normal[xref:${item.__item_context.page}#${item.anchor}[\`${item.fullName}\`]]`;
  26. }
  27. return res;
  28. }
  29. module.exports['with-prelude'] = (opts) => {
  30. const links = getAllLinks(opts.data.site.items);
  31. const contents = opts.fn();
  32. const neededLinks = contents
  33. .match(/\{[-._a-z0-9]+\}/ig)
  34. .map(m => m.replace(/^\{(.+)\}$/, '$1'))
  35. .filter(k => k in links);
  36. const prelude = neededLinks.map(k => `:${k}: ${links[k]}`).join('\n');
  37. return prelude + '\n' + contents;
  38. };