From 625d8378458a9c0f13eb6b745061e73a5e124a3c Mon Sep 17 00:00:00 2001
From: Lars Trieloff <trieloff@adobe.com>
Date: Wed, 13 Dec 2017 20:28:06 +0000
Subject: [PATCH] #14 list schema hierarchy

---
 examples/docs/README.md                       |  2 +
 examples/docs/abstract.schema.md              |  4 +-
 examples/docs/custom.schema.md                |  4 +-
 examples/docs/deepextending.schema.md         | 26 +++++++++++
 examples/docs/definitions.schema.md           |  4 +-
 examples/docs/example.schema.md               |  4 +-
 examples/docs/extending.schema.md             | 25 +++++++++++
 examples/docs/extensible.schema.md            |  4 +-
 examples/docs/simple.schema.md                |  4 +-
 examples/docs/subdir/subdir.schema.md         |  4 +-
 .../deepextending.schema.json                 | 43 +++++++++++++++++++
 .../generated-schemas/extending.schema.json   | 39 +++++++++++++++++
 examples/schemas/deepextending.schema.json    | 35 +++++++++++++++
 examples/schemas/extending.schema.json        | 33 ++++++++++++++
 lib/markdownWriter.js                         | 15 ++++++-
 templates/md/header.ejs                       | 16 ++++++-
 16 files changed, 252 insertions(+), 10 deletions(-)
 create mode 100644 examples/docs/deepextending.schema.md
 create mode 100644 examples/docs/extending.schema.md
 create mode 100644 examples/generated-schemas/deepextending.schema.json
 create mode 100644 examples/generated-schemas/extending.schema.json
 create mode 100644 examples/schemas/deepextending.schema.json
 create mode 100644 examples/schemas/extending.schema.json

diff --git a/examples/docs/README.md b/examples/docs/README.md
index be74562..3af4eca 100644
--- a/examples/docs/README.md
+++ b/examples/docs/README.md
@@ -8,8 +8,10 @@
 
 * [Abstract](./abstract.schema.md) – `https://example.com/schemas/abstract`
 * [Custom](./custom.schema.md) – `https://example.com/schemas/custom`
+* [Custom](./deepextending.schema.md) – `https://example.com/schemas/deepextending`
 * [Definitions](./definitions.schema.md) – `https://example.com/schemas/definitions`
 * [Example](./example.schema.md) – `https://example.com/schemas/example`
+* [Extending](./extending.schema.md) – `https://example.com/schemas/extending`
 * [Extensible](./extensible.schema.md) – `https://example.com/schemas/extensible`
 * [Simple](./simple.schema.md) – `https://example.com/schemas/simple`
 
diff --git a/examples/docs/abstract.schema.md b/examples/docs/abstract.schema.md
index ea39f5b..4544fbd 100644
--- a/examples/docs/abstract.schema.md
+++ b/examples/docs/abstract.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Abstract
+# Abstract Schema
+
 ```
 https://example.com/schemas/abstract
 ```
@@ -14,4 +15,5 @@ This is an abstract schema. It has `definitions`, but does not declare any prope
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Cannot be instantiated | Yes | Forbidden | [abstract.schema.json](abstract.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/docs/custom.schema.md b/examples/docs/custom.schema.md
index c81635f..9a4bc1c 100644
--- a/examples/docs/custom.schema.md
+++ b/examples/docs/custom.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Custom
+# Custom Schema
+
 ```
 https://example.com/schemas/custom
 ```
@@ -14,4 +15,5 @@ This is an extensible schema. It has `definitions`, that can be used in other sc
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Can be instantiated | Yes | Allowed | [custom.schema.json](custom.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/docs/deepextending.schema.md b/examples/docs/deepextending.schema.md
new file mode 100644
index 0000000..ce410cf
--- /dev/null
+++ b/examples/docs/deepextending.schema.md
@@ -0,0 +1,26 @@
+
+---
+template: reference
+foo: bar
+---
+
+# Custom Schema
+
+```
+https://example.com/schemas/deepextending
+```
+
+This is an extending schema. It is extending another extending schema. It pulls `definitions` from other schemas.
+
+| Abstract | Extensible | Custom Properties | Defined In |
+|----------|------------|-------------------|------------|
+| Can be instantiated | Yes | Forbidden | [deepextending.schema.json](deepextending.schema.json) |
+
+## Schema Hierarchy
+
+* Custom `schema.$id`
+ * [Extensible](extensible.schema.md) `https://example.com/schemas/extensible`
+ * [Definitions](definitions.schema.md) `https://example.com/schemas/definitions`
+ * [Extending](extending.schema.md) `https://example.com/schemas/extending`
+
+---
\ No newline at end of file
diff --git a/examples/docs/definitions.schema.md b/examples/docs/definitions.schema.md
index f1c52de..b96fbda 100644
--- a/examples/docs/definitions.schema.md
+++ b/examples/docs/definitions.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Definitions
+# Definitions Schema
+
 ```
 https://example.com/schemas/definitions
 ```
@@ -15,4 +16,5 @@ It is imported using `allOf` and `$ref`.
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Can be instantiated | Yes | Forbidden | [definitions.schema.json](definitions.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/docs/example.schema.md b/examples/docs/example.schema.md
index 8c3bfa8..9b5260f 100644
--- a/examples/docs/example.schema.md
+++ b/examples/docs/example.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Example
+# Example Schema
+
 ```
 https://example.com/schemas/example
 ```
@@ -14,4 +15,5 @@ This is an example schema with examples. Too many examples? There can never be t
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Can be instantiated | No | Forbidden | [example.schema.json](example.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/docs/extending.schema.md b/examples/docs/extending.schema.md
new file mode 100644
index 0000000..b64a706
--- /dev/null
+++ b/examples/docs/extending.schema.md
@@ -0,0 +1,25 @@
+
+---
+template: reference
+foo: bar
+---
+
+# Extending Schema
+
+```
+https://example.com/schemas/extending
+```
+
+This is an extending schema. It pulls `definitions` from other schemas.
+
+| Abstract | Extensible | Custom Properties | Defined In |
+|----------|------------|-------------------|------------|
+| Can be instantiated | Yes | Forbidden | [extending.schema.json](extending.schema.json) |
+
+## Schema Hierarchy
+
+* Extending `schema.$id`
+ * [Extensible](extensible.schema.md) `https://example.com/schemas/extensible`
+ * [Definitions](definitions.schema.md) `https://example.com/schemas/definitions`
+
+---
\ No newline at end of file
diff --git a/examples/docs/extensible.schema.md b/examples/docs/extensible.schema.md
index 735a598..8073e9f 100644
--- a/examples/docs/extensible.schema.md
+++ b/examples/docs/extensible.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Extensible
+# Extensible Schema
+
 ```
 https://example.com/schemas/extensible
 ```
@@ -14,4 +15,5 @@ This is an extensible schema. It has `definitions`, that can be used in other sc
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Cannot be instantiated | Yes | Forbidden | [extensible.schema.json](extensible.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/docs/simple.schema.md b/examples/docs/simple.schema.md
index 7a9d099..20ef824 100644
--- a/examples/docs/simple.schema.md
+++ b/examples/docs/simple.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Simple
+# Simple Schema
+
 ```
 https://example.com/schemas/simple
 ```
@@ -14,4 +15,5 @@ This is a *very* simple example of a JSON schema. There is only one property.
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Can be instantiated | No | Forbidden | [simple.schema.json](simple.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/docs/subdir/subdir.schema.md b/examples/docs/subdir/subdir.schema.md
index b7fee1d..a8f3786 100644
--- a/examples/docs/subdir/subdir.schema.md
+++ b/examples/docs/subdir/subdir.schema.md
@@ -4,7 +4,8 @@ template: reference
 foo: bar
 ---
 
-# Subdir
+# Subdir Schema
+
 ```
 https://example.com/schemas/subdir/subdir
 ```
@@ -14,4 +15,5 @@ A schema in a sub directory
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Cannot be instantiated | Yes | Forbidden | [subdir/subdir.schema.json](subdir/subdir.schema.json) |
+
 ---
\ No newline at end of file
diff --git a/examples/generated-schemas/deepextending.schema.json b/examples/generated-schemas/deepextending.schema.json
new file mode 100644
index 0000000..b5228aa
--- /dev/null
+++ b/examples/generated-schemas/deepextending.schema.json
@@ -0,0 +1,43 @@
+{
+    "meta:license": [
+        "Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
+        "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
+        "you may not use this file except in compliance with the License. You may obtain a copy",
+        "of the License at http://www.apache.org/licenses/LICENSE-2.0"
+    ],
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "$id": "https://example.com/schemas/deepextending",
+    "title": "Custom",
+    "description": "This is an extending schema. It is extending another extending schema. It pulls `definitions` from other schemas.",
+    "type": "object",
+    "meta:extensible": true,
+    "meta:extends": [
+        "https://example.com/schemas/definitions",
+        "https://example.com/schemas/extensible",
+        "https://example.com/schemas/extending"
+    ],
+    "definitions": {
+        "fourth": {
+            "properties": {
+                "hey": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        }
+    },
+    "allOf": [
+        {
+            "$ref": "https://example.com/schemas/extensible#/definitions/second"
+        },
+        {
+            "$ref": "https://example.com/schemas/definitions#/definitions/myid"
+        },
+        {
+            "$ref": "https://example.com/schemas/extending#/definitions/third"
+        },
+        {
+            "$ref": "#/definitions/fourth"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/examples/generated-schemas/extending.schema.json b/examples/generated-schemas/extending.schema.json
new file mode 100644
index 0000000..41b19a2
--- /dev/null
+++ b/examples/generated-schemas/extending.schema.json
@@ -0,0 +1,39 @@
+{
+    "meta:license": [
+        "Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
+        "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
+        "you may not use this file except in compliance with the License. You may obtain a copy",
+        "of the License at http://www.apache.org/licenses/LICENSE-2.0"
+    ],
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "$id": "https://example.com/schemas/extending",
+    "title": "Extending",
+    "description": "This is an extending schema. It pulls `definitions` from other schemas.",
+    "type": "object",
+    "meta:extensible": true,
+    "meta:extends": [
+        "https://example.com/schemas/definitions",
+        "https://example.com/schemas/extensible"
+    ],
+    "definitions": {
+        "third": {
+            "properties": {
+                "baz": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        }
+    },
+    "allOf": [
+        {
+            "$ref": "https://example.com/schemas/extensible#/definitions/second"
+        },
+        {
+            "$ref": "https://example.com/schemas/definitions#/definitions/myid"
+        },
+        {
+            "$ref": "#/definitions/third"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/examples/schemas/deepextending.schema.json b/examples/schemas/deepextending.schema.json
new file mode 100644
index 0000000..fd0f03e
--- /dev/null
+++ b/examples/schemas/deepextending.schema.json
@@ -0,0 +1,35 @@
+{
+    "meta:license": [
+        "Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
+        "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
+        "you may not use this file except in compliance with the License. You may obtain a copy",
+        "of the License at http://www.apache.org/licenses/LICENSE-2.0"
+    ],
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "$id": "https://example.com/schemas/deepextending",
+    "title": "Custom",
+    "description": "This is an extending schema. It is extending another extending schema. It pulls `definitions` from other schemas.",
+    "type": "object",
+    "meta:extensible": true,
+    "meta:extends": [
+      "https://example.com/schemas/definitions",
+      "https://example.com/schemas/extensible",
+      "https://example.com/schemas/extending"
+    ],
+    "definitions": {
+        "fourth": {
+            "properties": {
+                "hey": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        }
+    },
+    "allOf": [
+      { "$ref": "https://example.com/schemas/extensible#/definitions/second"},
+      { "$ref": "https://example.com/schemas/definitions#/definitions/myid" },
+      { "$ref": "https://example.com/schemas/extending#/definitions/third" },
+      { "$ref": "#/definitions/fourth" }
+    ]
+}
\ No newline at end of file
diff --git a/examples/schemas/extending.schema.json b/examples/schemas/extending.schema.json
new file mode 100644
index 0000000..b58a768
--- /dev/null
+++ b/examples/schemas/extending.schema.json
@@ -0,0 +1,33 @@
+{
+    "meta:license": [
+        "Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
+        "This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
+        "you may not use this file except in compliance with the License. You may obtain a copy",
+        "of the License at http://www.apache.org/licenses/LICENSE-2.0"
+    ],
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "$id": "https://example.com/schemas/extending",
+    "title": "Extending",
+    "description": "This is an extending schema. It pulls `definitions` from other schemas.",
+    "type": "object",
+    "meta:extensible": true,
+    "meta:extends": [
+      "https://example.com/schemas/definitions",
+      "https://example.com/schemas/extensible"
+    ],
+    "definitions": {
+        "third": {
+            "properties": {
+                "baz": {
+                    "type": "string",
+                    "description": "A unique identifier given to every addressable thing."
+                }
+            }
+        }
+    },
+    "allOf": [
+      { "$ref": "https://example.com/schemas/extensible#/definitions/second"},
+      { "$ref": "https://example.com/schemas/definitions#/definitions/myid" },
+      { "$ref": "#/definitions/third" }
+    ]
+}
\ No newline at end of file
diff --git a/lib/markdownWriter.js b/lib/markdownWriter.js
index 51d91e6..d7ba957 100644
--- a/lib/markdownWriter.js
+++ b/lib/markdownWriter.js
@@ -41,7 +41,6 @@ function custom(schema) {
 }
 
 function schemaProps(schema, schemaPath, filename) {
-  console.log(_.keys(schema));
   return {
     // if there are definitions, but no properties
     abstract: (schema.definitions !== undefined && _.keys(schema.properties).length === 0) ? 'Cannot be instantiated' : 'Can be instantiated',
@@ -51,6 +50,18 @@ function schemaProps(schema, schemaPath, filename) {
   };
 }
 
+function flatten(dependencies) {
+  let deps = [];
+  if (dependencies) {
+    const key = _.keys(dependencies)[0];
+    deps = _.toPairs(dependencies[key]).map(([ first, second ]) => {
+      second.$id = first;
+      return second;
+    });
+  }
+  return deps;
+}
+
 const generateMarkdown = function(filename, schema, schemaPath, outDir, dependencyMap) {
   var ctx = {
     schema: schema,
@@ -60,6 +71,7 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen
   };
 
   console.log(filename);
+  //console.log(dependencyMap);
 
   // this structure allows us to have separate templates for each element. Instead of having
   // one huge template, each block can be built individually
@@ -67,6 +79,7 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen
     [ 'frontmatter.ejs', { meta: schema.metaElements } ],
     [ 'header.ejs', {
       schema: schema,
+      dependencies: flatten(dependencyMap),
       props: schemaProps(schema, schemaPath, filename) } ],
     //[ 'divider.ejs', null ],
     //[ 'topSchema.ejs', ctx ],
diff --git a/templates/md/header.ejs b/templates/md/header.ejs
index 13e72e2..d2c2365 100644
--- a/templates/md/header.ejs
+++ b/templates/md/header.ejs
@@ -4,7 +4,9 @@
  * you may not use this file except in compliance with the License. You may obtain a copy
  * of the License at http://www.apache.org/licenses/LICENSE-2.0
  */ %>
-# <%=schema.title %>
+# <%=schema.title %> Schema
+
+
 ```
 <%= schema.$id %>
 ```
@@ -16,4 +18,14 @@
 
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
-| <%=props.abstract %> | <%=props.extensible %> | <%=props.custom %> | [<%=props.original %>](<%=props.original %>) |
\ No newline at end of file
+| <%=props.abstract %> | <%=props.extensible %> | <%=props.custom %> | [<%=props.original %>](<%=props.original %>) |
+
+<% if (dependencies.length > 0) { %>
+## Schema Hierarchy
+
+
+* <%=schema.title %> `schema.$id`
+<%  _.forEach(dependencies, ({$linkVal, $linkPath, $id}) => { %>
+ * [<%= $linkVal %>](<%=$linkPath %>) `<%= $id %>`
+<% }); %>
+<% } %>
\ No newline at end of file
-- 
GitLab