New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

builddocs

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

builddocs - npm Package Compare versions

Comparing version 0.3.2 to 1.0.0

templates/enum.html

20

bin/builddocs.js

@@ -6,6 +6,6 @@ #!/usr/bin/env node

var name, main, templates, allowUnresolved = true, files = [], format = "html"
var name, main, templates, allowUnresolvedTypes = true, filename, format = "html"
function help(status) {
console.log("Usage: builddocs --name <name> --main <main> [--templates <templatedir>]\n [--disallow-unresolved] [--help] [--format <format>] <sourcefiles>")
console.log("Usage: builddocs [--name <name>] [--main <main>] [--templates <templatedir>]\n [--disallow-unresolved] [--help] [--format <format>] <filename>")
process.exit(status)

@@ -19,17 +19,17 @@ }

else if (arg == "--templates") templates = process.argv[++i]
else if (arg == "--disallow-unresolved") allowUnresolved = false
else if (arg == "--disallow-unresolved") allowUnresolvedTypes = false
else if (arg == "--format") format = process.argv[++i]
else if (arg.charAt(0) != "-") files.push(arg)
else if (arg.charAt(0) != "-" && !filename) filename = arg
else help(arg == "--help" ? 0 : 1)
}
if (!main || !name || !files.length) help(1)
if (!filename) help(1)
console.log(build({
name: name,
main: main,
files: files.join(" "),
templates: templates,
name,
main,
filename,
templates,
format,
allowUnresolvedTypes: allowUnresolved
allowUnresolvedTypes
}))
{
"name": "builddocs",
"version": "0.3.2",
"description": "Build documentation files from getdocs-commented source code",
"version": "1.0.0",
"description": "Build documentation files from commented source code",
"main": "src/builddocs.js",

@@ -30,4 +30,3 @@ "repository": {

"dependencies": {
"getdocs": "^0.6.0",
"glob": "^7.0.3",
"getdocs-ts": "^0.1.0",
"markdown-it": "^6.0.5",

@@ -34,0 +33,0 @@ "markdown-it-deflist": "^2.0.0",

# builddocs
This is a utility that transforms code documented with
[getdocs](https://github.com/marijnh/getdocs)-style doc comments into
HTML.
[getdocs](https://github.com/marijnh/getdocs-ts)-style doc comments
into HTML.
It exports the following values:
**`build`**`: (config: Object, data: ?Object) → string`
**`build`**`: (config: Object, items: ?Object) → string`

@@ -16,12 +16,14 @@ Build the documentation for a given set of files. The configuration

* **`files`**`: string` Should be a space-separated set of path
strings, which may contain wildcards. These are the files that are
scanned for doc comments.
* **`filename`**`: string` If `items` isn't given, this should point
at the main filename to extract docs from.
* **`main`**`: string` The path to the main template, which should be
a Markdown file with `@itemName` placeholders where the generated
docs for the documented items in the source should be inserted.
builddocs will complain when the set of item placeholders does not
match the set of documented items.
* **`main`**`: ?string` The path to the main template, which should
be a Markdown file with `@itemName` placeholders where the
generated docs for the documented items in the source should be
inserted. builddocs will complain when the set of item placeholders
does not match the set of documented items. When not given, the
items will be output in the order in which they are found.
* **`mainText`**`: ?string` The main template as a string.
* **`anchorPrefix`**`: ?string` Can be used to override the prefix

@@ -32,3 +34,3 @@ used when generating HTML anchors. Defaults to the module name with

* **`imports`**`: ?[Object]` A set of object mapping type names to
* **`imports`**`: ?[Object | (item: Object) → ?string]` A set of object mapping type names to
URLs. Will make the library recognize the given type names and

@@ -55,21 +57,25 @@ properly link them.

The second parameter, `data`, can be used if the data for the module
has already been read. By default, `build` will read it.
* **`breakAt`**`: ?number` When given, type or property lists whose
(estimated) length is equal to or greater than the given value will
be wrapped in a `<div class=breaktype>` element (which can be
styled with a left padding to indent it).
**`read`**`: (config: Object) → Object`
* **`processType`**`: ?(type: Type) => ?Type` When given, types will
be passed through this function before being formatted. It can
return a replacement JSON structure for the type.
Read comments from a given set of files. `config` has the same shape
as the argument to `build` (though only `files` and `order` will be
read by this function).
The second parameter, `items`, can be used if the JSON data for the
module has already been read. By default, `build` will read it using
[`getdocs-ts`](https://github.com/marijnh/getdocs-ts).
The returned object has the following properties:
**`read`**`: (config: Object) → Object`
* **`items`**`: Object` The data returned by
[getdocs](https://github.com/marijnh/getdocs).
Read types and comments from a given set of files. `config` has the
same shape as the argument to `build` (though only `files` and `order`
will be read by this function).
* **`pieces`**`: [Object]` An ordered array of getdocs items and
`!!`-prefixed comments that were found in the code.
The function returns the data returned by
[getdocs-ts](https://github.com/marijnh/getdocs-ts), an object containing
metadata for each of the items documented in the source files.
* **`all`**`: Object` A mapping from getdocs ids to getdocs items.
**`browserImports`**`: Object<string>`

@@ -76,0 +82,0 @@

@@ -6,2 +6,4 @@ module.exports = {

Document: "https://developer.mozilla.org/en/docs/DOM/document",
ShadowRoot: "https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot",
DocumentOrShadowRoot: "https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot",
XMLDocument: "https://developer.mozilla.org/en/docs/Parsing_and_serializing_XML",

@@ -105,3 +107,4 @@ HTMLElement: "https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement",

MutationRecord: "https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord",
MutationObserver: "https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver"
MutationObserver: "https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver",
HTMLElementEventMap: "https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_dom_d_.htmlelementeventmap.html"
}
let fs = require("fs")
let Mold = require("mold-template")
let read = exports.read = require("./read").read
let builtins = require("./builtins")

@@ -8,4 +7,4 @@

exports.build = function(config, data) {
if (!data) data = read(config)
exports.build = function(config, items) {
if (!items) items = require("getdocs-ts").gather(config)

@@ -16,17 +15,31 @@ let format = config.format || "html"

format == "markdown" ? (() => {
let mold = loadMarkdownTemplates(config, data)
return name => mold.defs.item({item: data.items[name], name}).replace(/[\n␤]{2}$/g, "\n")
let mold = loadMarkdownTemplates(config, items)
return name => mold.defs.item({item: items[name], name}).replace(/[\n␤]{2}$/g, "\n")
})()
: null
let placed = Object.create(null)
let main = fs.readFileSync(config.main, "utf8").replace(/(^|\n)@(\w+)(?=$|\n)/g, function(_, before, name) {
if (placed[name]) throw new Error("Item " + name + " is included in doc template twice")
if (!data.items[name]) throw new Error("Unknown item " + name + " included in doc template")
placed[name] = true
return before + renderItem(name)
})
for (let name in data.items) if (!placed[name])
throw new Error("Item " + name + " is missing from the doc template")
let instantiateTemplate = template => {
if (format == "html") template = template.replace(/(^|\n)(@\w+\n+)*@\w+(?=$|\n)/g, "<dl>\n$&\n</dl>")
let placed = Object.create(null), problems = []
let result = template.replace(/(^|\n)@(\w+)(?=$|\n)/g, function(_, before, name) {
if (placed[name]) problems.push("Item " + name + " is included in doc template twice")
if (!items[name]) problems.push("Unknown item " + name + " included in doc template")
placed[name] = true
return before + renderItem(name)
})
for (let name in items) if (!placed[name])
problems.push("Item " + name + " is missing from the doc template")
return {result, problems}
}
let main
if (config.main || config.mainText) {
let {problems, result} = instantiateTemplate(config.mainText || fs.readFileSync(config.main, "utf8"))
if (problems.length)
for (let prob of problems) console.log(prob)
else
main = result
}
if (!main) main = instantiateTemplate(Object.keys(items).map(name => "@" + name).join("\n\n")).result
if (format == "markdown") {

@@ -38,7 +51,7 @@ return main.replace(/␤/g, "\n")

let markdown = require("markdown-it")(mdOptions).use(require("markdown-it-deflist"))
let mold = loadHTMLTemplates(markdown, config, data)
let mold = loadHTMLTemplates(markdown, config, items)
let doc = markdown.render(main)
return doc.replace(/<div data-item="([^"]+)"><\/div>/g, function(_, name) {
return mold.defs.item({item: data.items[name], name})
return mold.defs.item({item: items[name], name})
})

@@ -50,3 +63,3 @@ }

let prefix = config.anchorPrefix
if (prefix == null) prefix = config.name + "."
if (prefix == null) prefix = config.name ? config.name + "." : ""
return prefix

@@ -58,3 +71,3 @@ }

let match = /^(.*?)\.(\w+)$/.exec(filename)
if (match && match[2] == ext && !(match[1] in mold.defs))
if (match && match[2] == ext && !has(mold.defs, match[1]))
mold.bake(match[1], fs.readFileSync(dir + "/" + filename, "utf8").trim())

@@ -64,4 +77,4 @@ })

function loadHTMLTemplates(markdown, config, data) {
let mold = new Mold(moldEnv(config, data))
function loadHTMLTemplates(markdown, config, items) {
let mold = new Mold(moldEnv(config, items))
mold.defs.markdown = function(text) {

@@ -81,4 +94,4 @@ if (!text) return ""

function loadMarkdownTemplates(config, data) {
let mold = new Mold(moldEnv(config, data))
function loadMarkdownTemplates(config, items) {
let mold = new Mold(moldEnv(config, items))

@@ -94,14 +107,28 @@ if (config.templates) templateDir(mold, config.templates, "md")

function maybeLinkType(config, data, name) {
if (name in data.all) return "#" + prefix(config) + name
if (name.charAt(0) == '"') return false
function has(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop)
}
function isLiteral(type) {
return /^(\"|\'|-?\d)/.test(type)
}
function maybeLinkType(config, items, type) {
let name = type.type
if (type.typeParamSource) return "#" + prefix(config) + type.typeParamSource
if (has(items, name) && items[name].kind != "reexport") return "#" + prefix(config) + name
if (isLiteral(name)) return false
let imports = config.imports, qualified = config.qualifiedImports
if (imports) for (let i = 0; i < imports.length; i++) {
let set = imports[i]
if (Object.prototype.hasOwnProperty.call(set, name))
if (typeof set == "function") {
let result = set(type)
if (result) return result
} else if (has(set, name)) {
return set[name]
}
}
if (qualified) for (let pref in qualified) if (name.indexOf(pref + ".") == 0) {
let inner = name.slice(pref.length + 1)
if (Object.prototype.hasOwnProperty.call(qualified[pref], inner))
if (has(qualified[pref], inner))
return qualified[pref][inner]

@@ -112,9 +139,9 @@ }

function moldEnv(config, data) {
function moldEnv(config, items) {
let env = {
prefix: prefix(config),
linkType: function(type) {
let link = maybeLinkType(config, data, type.type)
let link = maybeLinkType(config, items, type)
if (!link && link !== false && !config.allowUnresolvedTypes)
throw new Error("Unknown type '" + type.type + "' at " + type.loc.file + ":" + type.loc.line)
throw new Error("Unknown type '" + type.type + "'" + (type.loc ? " at " + type.loc.file + ":" + type.loc.line : ""))
return link

@@ -130,2 +157,8 @@ },

return false
},
breakType: function(type) {
return config.breakAt != null && typeLen(type) >= config.breakAt
},
processType: function(type) {
return (config.processType && config.processType(type)) || type
}

@@ -136,1 +169,33 @@ }

}
const typeLenMap = {
union: 1,
intersection: 1,
tuple: 2,
Array: 2,
ReadonlyArray: 11,
indexed: 2,
conditional: 6,
mapped: 10,
Function: 4
}
// Estimate the length of a type
function typeLen(type, extra = 0) {
if (!type) return 0
if (Array.isArray(type)) return type.reduce((compl, t, i) => compl + typeLen(t) + (i ? 2 : 0), extra)
let val = extra + (typeLenMap.hasOwnProperty(type.type) ? typeLenMap[type.type] : type.type.length)
if (type.kind == "parameter") val += (type.name?.length || 0) + 2
val += typeLen(type.implements) +
(type.default ? type.default.length + 3 : 0) +
typeLen(type.typeArgs, 2)
if (type.signatures) {
let sig = type.signatures[0]
val += typeLen(sig.params) + typeLen(sig.typeArgs, 2) + typeLen(sig.returns, 3)
}
if (type.properties) for (let name in type.properties) {
let prop = type.properties[name]
if (!prop.description) val += name.length + 2 + typeLen(prop)
}
return val
}

@@ -5,3 +5,10 @@ module.exports = {

boolean: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
true: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
false: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
number: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
Infinity: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity",
"-Infinity": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity",
NaN: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN",
undefined: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined",
null: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null",
String: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String",

@@ -11,5 +18,8 @@ Boolean: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",

Iterator: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols",
Iterable: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol",
Array: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array",
ReadonlyArray: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array",
Object: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object",
RegExp: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp",
RegExpMatchArray: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Return_value",
Function: "https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function",

@@ -42,6 +52,25 @@ Date: "https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date",

WeakSet: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet",
Exclude: "https://www.typescriptlang.org/docs/handbook/utility-types.html#excludetype-union",
Extract: "https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union",
InstanceType: "https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype",
NonNullable: "https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype",
Omit: "https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys",
Partial: "https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype",
Pick: "https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys",
Readonly: "https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype",
Record: "https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype",
Required: "https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype",
ReturnType: "https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype",
ThisType: "https://www.typescriptlang.org/docs/handbook/utility-types.html#thistypetype",
class: false,
interface: false,
any: false,
union: false
unknown: false,
never: false,
union: false,
tuple: false,
typeparameter: false,
intersection: false,
conditional: false,
mapped: false
}
<<in {item, name}>>
### <<h item.type>> <<h name>><<if item.extends>> extends <<type item.extends>><</if>>␤␤
### <<if item.abstract>>abstract <</if>><<h item.kind == "typealias" ? "type" : item.kind>> <<h name>>
<<if item.typeParams>>`<<typeparams item>><</if>>
<<if item.extends>> extends <<type item.extends>><</if>>␤␤
<<for impl item.implements || []
>> <<t item.kind == "interface" ? "extends" : "implements">> `<<type impl>>`
<</for>>
<<if item.description>><<h item.description>>␤␤<</if>>
<<if item.constructor>>
<<define {item: item.constructor, name: name}>>
<<if item.construct>>
<<define {item: item.construct, name: name}>>
<</if>>
<<if item.instanceProperties>>
<<for name, prop in item.instanceProperties>><<define {item: prop, name: name}>><</for>>
<</if>>
<<if item.properties>>
<<for name, prop in item.properties>><<define {item: prop, name: name}>><</for>>
<<for name, prop in item.properties>><<define {item: prop, name: name, static: item.kind == "class"}>><</for>>
<</if>>
<<if item.staticProperties>>
<<for name, prop in item.staticProperties>><<define {item: prop, name: name, static: true}>><</for>>
<</if>>

@@ -1,9 +0,12 @@

<<in {item, name, static, depth=0}>>
<<in {item, name, static, abstract, depth=0}>>
<<h " ".repeat(depth)>> * <<if static>>`static `<</if>>
<<h " ".repeat(depth)>> * <<if abstract>>`abstract `<</if>><<if static>>`static `<</if>>
<<if item.type == "Function" && !item.optional>>
<<if /\.constructor$/.test(item.id)>>`new `<</if>>**`<<h name>>`**`<<fntype item>>`
<<if item.signatures[0].type == "constructor">>`new `<</if>>**`<<h name>>`**`<<fntype item.signatures[0]>>`
<<else>>
**`<<h name>>`**<<if item.type>>`: <<type item>>`<</if>>
<</if>>
<<for sig item.signatures?.slice(1) || []>>
\␤<<h " ".repeat(depth + 3)>><<if sig.type == "constructor">>`new `<</if>>**`<<h name>>`**`<<fntype sig>>`
<</for>>
<<if item.description>>\␤<<indent {text: item.description, depth: depth + 3}>><</if>>␤␤

@@ -22,2 +25,2 @@ <<for name, prop in item.properties || {}>>

<<define {item: item.returns, name: "returns", depth: depth + 3}>>
<</if>>
<</if>>

@@ -0,1 +1,2 @@

<<typeparams $in>>
(<<for param $in.params || []>>

@@ -2,0 +3,0 @@ <<if $i>>, <</if>><<if param.rest>>...<</if>>

@@ -1,5 +0,13 @@

<<if $in.item.type == "class" || $in.item.type == "interface">>
<<in {item}>>
<<if item.kind == "class" || item.kind == "interface" || item.kind == "typealias" && item.type == "Object">>
<<class $in>>
<<elif item.kind == "typealias">>
<<typealias $in>>
<<elif item.kind == "enum">>
<<enum $in>>
<<elif item.kind == "reexport">>
<<reexport $in>>
<<else>>
<<define $in>>
<</if>>
<<do
var undocumentedProps = false
if ($in.properties) for (var n in $in.properties) if (!hasDescription($in.properties[n])) undocumentedProps = true
$in = processType($in)
var undocumentedProps = null
if ($in.properties) for (var n in $in.properties) {
var p = $in.properties[n]
if (!hasDescription(p)) (undocumentedProps || (undocumentedProps = [])).push([n, p])
}
>>
<<if $in.optional>>?<</if>>
<<if $in.type == "Function">>
fn<<fntype $in>>
fn<<fntype $in.signatures[0]>>
<<elif $in.type == "Array">>
[<<for elt $in.typeParams || []>><<if $i>>, <</if>><<type elt>><</for>>]
<<typeParens $in.typeArgs[0]>>[]
<<elif $in.type == "ReadonlyArray">>
readonly <<typeParens $in.typeArgs[0]>>[]
<<elif $in.type == "tuple">>
[<<for elt $in.typeArgs || []>><<if $i>>, <</if>><<type elt>><</for>>]
<<elif $in.type == "union">>
<<for elt $in.typeParams>><<if $i>> | <</if>><<type elt>><</for>>
<<elif undocumentedProps>>
<<do var needComma = false>>
{<<for name, prop in $in.properties>>
<<if !hasDescription(prop)>><<if needComma>>, <</if>><<h name>>: <<type prop>><<do needComma = true>><</if>>
<</for>>}
<<for elt $in.typeArgs>><<if $i>> | <</if>><<type elt>><</for>>
<<elif $in.type == "intersection">>
<<for elt $in.typeArgs>><<if $i>> & <</if>><<type elt>><</for>>
<<elif $in.type == "indexed">>
<<for elt $in.typeArgs>><<if $i>>[<</if>><<type elt>><<if $i>>]<</if>><</for>>
<<elif $in.type == "conditional">>
<<type $in.typeArgs[0]>> extends <<type $in.typeArgs[1]>> ? <<type $in.typeArgs[2]>> : <<type $in.typeArgs[3]>>
<<elif $in.type == "mapped">>
{[<<t $in.key.name>> in <<type $in.key.implements[0]>>]: <<type $in.typeArgs[0]>>}
<<elif $in.type == "keyof">>
keyof <<typeParens $in.typeArgs[0]>>
<<elif $in.type == "typeof">>
typeof <<typeParens $in.typeArgs[0]>>
<<elif undocumentedProps || $in.signatures>>
{<<if $in.signatures>>
<<if $in.signatures[0].type == "constructor">>new <</if>><<fntype $in.signatures[0]>>
<<if undocumentedProps>>, <</if>>
<</if>>
<<for prop undocumentedProps || []>>
<<t prop[0]>><<if prop[1].optional>>?<</if>>: <<type prop[1]>>
<<if $i < undocumentedProps.length - 1>>, <</if>>
<</for>>}
<<else>>
<<h $in.type>>
<<if $in.typeParams>>< <<for elt $in.typeParams>><<if $i>>, <</if>><<type elt>><</for>> ><</if>>
<<h $in.type>><<typeparams $in>>
<</if>>

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc