简体   繁体   English

如何轻松创建 Github 友好的 markdown 以记录 JavaScript 功能?

[英]How to easily create Github friendly markdown for documented JavaScript functions?

I want to be able to take JSDoc comments like this anywhere in JavaScript source (even nested down several layers of functions, in a module or even anonymous functions):我希望能够在 JavaScript 源代码中的任何地方使用这样的 JSDoc 注释(甚至嵌套在几个函数层中,在一个模块中,甚至是匿名函数中):

/**
 *  Used to do some important thing that needs doing that works like xyz.
 *  @param {String} whatever - some string that has some purpose
 *  @param {Function} callback - a function that needs to be run
 *  @returns {Boolean} whether or not something happened
 */
 function something(whatever, callback) {
     ...

and have some easy way to produce nice markdown:并有一些简单的方法来生成漂亮的 markdown:

##`root.something(whatever,callback)`
Used to do some important thing that needs doing that works like xyz.

*Parameters*
  `whatever {String}` some string that has some purpose
  `callback {Function}` a function that needs to be run

*Returns*
   `{Boolean}` whether or not something happened

Where "root" is the namespace at which that function is reachable.其中“root”是可以访问 function 的命名空间。 Or if it's an anonymous function or a private function (that for some reason should be in public doco, does that even make sense??), use some other convention to denote that.或者,如果它是匿名的 function 或私有的 function(出于某种原因应该在公共 doco 中,这甚至有意义吗??),请使用其他一些约定来表示它。 Maybe可能是

##_private_function_ `something(whatever,callback)`

  and

##_anonymous_function_`(whatever,callback)`

It doesn't have to be exactly that format, just something that looks good on Github and makes sense.它不必完全是那种格式,只要在 Github 上看起来不错并且有意义即可。 The ideal tool would be smart enough to be able to take code like Mustache.js and produce good output. And extra good would be if it can handle lots of source files and produce one document as output, or a linked set of documents depending on configuration.理想的工具应该足够智能,能够使用Mustache.js之类的代码并生成良好的 output。如果它可以处理大量源文件并生成一个文档 output 或一组链接的文档,则效果会更好,具体取决于配置。

And it would be best if this was done in a way that can be fully and easily included in a git repo so that people don't need to set up a highly specific toolchain to update the doco.最好是以一种可以完全轻松地包含在 git 存储库中的方式完成,这样人们就不需要设置高度特定的工具链来更新 doco。 Or require a minimal toolchain at least.或者至少需要一个最小的工具链。

Oh, and a pony.哦,还有一匹小马。


Existing options现有选项

JSDoc , plus some kind of HTML -> markdown conversion JSDoc ,加上某种 HTML -> markdown 转换

JSDoc is pretty good. JSDoc 非常好。 But I can't seem to make it work well with modules.但我似乎无法让它与模块一起工作。 Or rather, it's a bigger hassle than it ought to be IMHO.或者更确切地说,这比恕我直言应该更麻烦。 I shouldn't need to add an extra tag to name the function. I've tried the @export and @name and still have trouble getting it to show up in the final doc the way I'd want.我不需要添加额外的标签来命名 function。我已经尝试了 @export 和 @name,但仍然无法按照我想要的方式将其显示在最终文档中。 If someone can point to a JSDoc commented source that has modules in it and is done well, that might help.如果有人可以指向一个 JSDoc 注释源,其中包含模块并且做得很好,那可能会有所帮助。 Update: JSDoc v3 actually seems much better with modules than v2 so this might be a better fit.更新: JSDoc v3 实际上似乎比 v2 更适合模块,所以这可能更合适。

Even if I could get JSDoc output like I want, I'd need to convert from HTML to markdown. I can't seem to find a good tool for that, does one exist?即使我可以像我想要的那样获得 JSDoc output,我也需要从 HTML 转换为 markdown。我似乎找不到一个好的工具,是否存在?

Docdown文档下载

I played a bit with Docdown but the fact it's PHP is kind of a nonstarter for me...我玩过 Docdown 但事实上它是 PHP 对我来说有点不可能......

YUIDoc , plus conversion YUIDoc , 加上转换

I actually haven't played with YUIDoc but it looks ok.我实际上还没有玩过 YUIDoc,但它看起来不错。 Still, I would need a converter.尽管如此,我还是需要一个转换器。 Does it deal with modules easily and avoid having to supply the function name and export name explicitly?它是否轻松处理模块并避免必须显式提供 function 名称和导出名称?

Dox , plus markdown templates Dox ,加上 markdown 模板

Dox produces JSON as it's output, so then you'd need to marry that to some good markdown templates, and plus include a template engine to generate the documents. Dox 生成 JSON,因为它是 output,因此您需要将其与一些好的 markdown 模板结合,并包括一个模板引擎来生成文档。 Has anyone put together a set of such templates in a useful fashion?有没有人以有用的方式将一组这样的模板放在一起?

jGrouse , plus conversion jGrouse ,加上转换

Runs with ANT. Next...使用 ANT 运行。接下来...

ScriptDoc...脚本文档...

Does this even exist anymore?这甚至还存在吗? Seems to be part of Aptana studio so that would be a nonstarter... Aptana doesn't seem to any info on it.似乎是 Aptana studio 的一部分,所以这将是一个失败者...... Aptana 似乎没有任何关于它的信息。 But ScriptDoc.org has some interesting information on crack, if that's helpful...但是 ScriptDoc.org 有一些关于 crack 的有趣信息,如果有帮助的话......

Pdoc文档

Pdoc is Ruby based but that toolchain is not uncommon so that's not a huge problem. Pdoc 基于 Ruby,但该工具链并不少见,因此这不是一个大问题。 You can provide your own templates so maybe there are already some good markdown ones.您可以提供自己的模板,这样也许已经有一些不错的 markdown 模板了。 I haven't played with it... is it worthwhile?我没玩过它...它值得吗? Are there good markdown templates out there?那里有好的 markdown 模板吗?

Something else?还有别的吗?

What else is out there?外面还有什么?

Make your own!自己做!

After messing around with JSDoc for a few hours trying to make it work how I wanted, I gave up and wrote my own quick and dirty solution in Java for CharFunk , a unicode JavaScript library I've been working on.在与 JSDoc 搞了几个小时试图让它按照我想要的方式工作之后,我放弃了并在 Java 中为CharFunk 编写了我自己的快速而肮脏的解决方案,这是一个我一直在研究的 unicode JavaScript 库。 It works well enough for what I need though it's not close to general purpose yet.虽然它还没有接近通用目的,但它足以满足我的需要。


So.....所以.....

Is this an unmet need or is it just me?这是一个未满足的需求还是只是我?

I use jsdoc-to-markdown .. 我用jsdoc-to-markdown ..

write documented code: 写文档代码:

/**
a quite wonderful function
@param {object} - privacy gown
@param {object} - security
@returns {survival}
*/
function protection(cloak, dagger){}

get markdown docs: 得到降价文档:

$ jsdoc2md example/function.js

#protection(cloak, dagger)
a quite wonderful function

**Params**

- cloak `object` - privacy gown
- dagger `object` - security

**Returns**: `survival`

These projects have readme files rendered by jsdoc2md : 这些项目有jsdoc2md呈现的自述文件:

Have you tried jsdox ? 你试过jsdox吗?

Its a node.js jsdoc to markdown generator. 它是一个node.js jsdoc到markdown生成器。

markdox可以从javascript代码生成markdown文档。

jsDox. jsDox。 https://github.com/sutoiku/jsdox Full parse using UglifyJS https://github.com/sutoiku/jsdox使用UglifyJS完整解析

Mox. 莫克斯。 https://github.com/tjchaplin/mox Several ready-to-run examples/templates. https://github.com/tjchaplin/mox几个可立即运行的示例/模板。

Both handle JSDoc/Dox formats. 两者都处理JSDoc / Dox格式。 Both have active development. 两者都有积极的发展。 For me, Mox wins because of the example suite. 对我来说,Mox因为示例套件而获胜。

OK. 好。 After some deliberation with myself, I'd go with DOX + Underscore/Whatever JS templating engine over Node. 经过我自己的一些考虑,我会选择DOX + Underscore / Whatever JS模板引擎。

Should be pretty simple. 应该很简单。 You can, possibly, even jam into Grunt or similar and have it ran under a watch task. 你可能甚至可能会遇到Grunt或类似的东西,并让它在监视任务下运行。

Dox is, from what I can recall, is relatively lightweight and has an npm package (IIRC). 据我所知,Dox相对轻巧,并且有一个npm包(IIRC)。

UPDATE: I think, after some experience, that I'd like to change my answer to YUIDoc. 更新:我想,经过一些经验,我想改变我对YUIDoc的回答。

Try using Verb . 尝试使用动词 In the simplest use case Verb will build a readme from a template using data from package.json. 在最简单的用例中,Verb将使用package.json中的数据从模板构建自述文件。

But verb also has advanced features if you need to generate multi-page TOCs, or create custom helpers, etc. 但是,如果您需要生成多页TOC或创建自定义帮助程序等,动词也具有高级功能。

Regarding API documentation, see this example readme generated using code comments from index.js . 关于API文档,请参阅此示例使用index.js代码注释生成的自述文件。 Click on the headings, those are auto-generated too. 点击标题,这些也是自动生成的。 Use this built-in helper to generate API docs from whatever file path is specified. 使用此内置帮助程序从指定的任何文件路径生成API文档。 You can also use glob patterns to pull in docs from multiple files. 您还可以使用glob模式从多个文件中提取文档。

Verb will build a .verb.md without any config. Verb将在没有任何配置的情况下构建.verb.md But if you need more, you can use a verbfile.js 但是如果你需要更多,你可以使用verbfile.js

I had the need of creating a API documentation out of JSDoc which should be easy to use and also supports modern frontend stacks. 我需要从JSDoc创建一个API文档,它应该易于使用,并且还支持现代前端堆栈。 Some of the mentioned libraries have issues with JS code transpiled into babeljs therefore you have to transpile your code with comments temporarily just to generate your markdown documentation. 一些提到的库存在JS代码转换为babeljs的问题,因此您必须暂时使用注释来转换代码以生成降价文档。

For such use case I found http://documentation.js.org/ quite useful as they have integrated support for BabelJs configurations so it takes care of generating markdown (JSON, HTML) from your JSDocs. 对于这样的用例,我发现http://documentation.js.org/非常有用,因为它们集成了对BabelJs配置的支持,因此它负责从JSDocs生成markdown(JSON,HTML)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM