bitovi / documentjs

The sophisticated documentation engine
https://documentjs.com/
MIT License
598 stars 381 forks source link

Custom tags | no way to require built-in helpers for use #226

Open rjgotten opened 8 years ago

rjgotten commented 8 years ago

There's a tags option on siteConfig objects to load custom defined tags from a doc project's own folders.

Loading custom tags is set-up in such a way that it is impossible to require and use the common built-in helper functions such as lib/tags/helpers/tagNameDescription without nasty workarounds.

Currently, I've resorted to creating a librequire.js module that crawls back up the stack of parent modules until it finds a module originating from a /node_modules/documentjs/ folder and then it uses its fully resolved filename to pull up relative references.

i.e.

// librequire.js
var parent = module;

while ( parent && !/[\\\/]node_modules[\\\/]documentjs[\\/]/.test( parent.filename )) {
  parent = parent.parent;
}

parent = /^(.*[\\\/]node_modules[\\\/]documentjs[\\\/]).*/.exec( parent.filename )[ 1 ];

module.exports = function( path ) {
  return require( parent + path );
}
// customtag.js
var
  librequire = require( "./librequire" ),
  typer = librequire( "./lib/tags/helpers/typer" ),
  tree = librequire( "./lib/tags/helpers/tree" ),
  namer = librequire( "./lib/tags/helpers/namer" ),
  tnd = librequire( "./lib/tags/helpers/typeNameDescription" );

This is not something you want to burden your framework users with, is it?