-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonld2html.js
136 lines (115 loc) · 4.01 KB
/
jsonld2html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*!
* Renders JSON-LD as HTML
*/
import mustache from 'mustache';
import getTemplate from './template_exporter.js';
/**
* Data Object used as transfer between data_object from jsonld file and the mustache template
*/
function Card(_type, _pictureURL, _iconName="image",_title, _content, _footer, _breadcrumbList){
this.type = _type;
this.pictureURL = _pictureURL;
this.iconName = _iconName;
this.title = _title;
this.content = _content;
this.footer = _footer;
this.breadcrumbList = _breadcrumbList;
}
var jsonld2html = {
name: 'jsonld2html.js',
version: '0.0.1'
}
// Mapping the fallback icon to schema type
const typeToIconMap = new Map();
typeToIconMap.set("NewsArticle","newspaper");
typeToIconMap.set("Article","comment");
typeToIconMap.set("MusicAlbum","compact-disc");
typeToIconMap.set("MusicRecording","music");
typeToIconMap.set("BusReservation","bus");
/**
* @param {object} entireObj - Object to search
* @þaram {string} keyToFind - the Key to search for a value
* @return {string} if a key exists the value of the key will returned
*/
function findValueFromKey(entireObj, keyToFind) {
let foundValue;
for (let keysKey of Object.keys(entireObj)) {
if (keysKey !== keyToFind) {
continue;
}
foundValue = (entireObj[keysKey]);
}
return foundValue;
}
/**
* finds Key Values in deep nests and returns the object, depth is not restricted !
*/
function findNestedObj(entireObj, keyToFind) {
let foundObj;
JSON.stringify(entireObj, (_, nestedValue) => {
if (nestedValue && nestedValue[keyToFind]){
foundObj = nestedValue;
}
return nestedValue;
});
if(foundObj != undefined) {return foundObj;}
else return null;
}
/**
* this function is used to find objects inside a specific
* it returns the object in which to key and val matching
*/
function findNestedObjWithValue(entireObj, keyToFind, valToFind) {
let foundObj;
JSON.stringify(entireObj, (_, nestedValue) => {
if (nestedValue && nestedValue[keyToFind] === valToFind) {
foundObj = nestedValue;
}
return nestedValue;
});
return foundObj;
}
function renderFromTemplate(jsonLd, template) {
let temp_card_obj = new Card();
temp_card_obj.type = findValueFromKey(jsonLd,"@type");
if(typeToIconMap.has(temp_card_obj.type)) {
temp_card_obj.iconName = typeToIconMap.get(temp_card_obj.type);
}
let logo_object = findNestedObj(jsonLd, "logo");
if(logo_object != null) {
temp_card_obj.pictureURL = logo_object.logo;
}
let image_object = findNestedObj(jsonLd,"image");
if(image_object != null) {
temp_card_obj.pictureURL = image_object.image;
}
let title_object = findNestedObj(jsonLd,"name");
if(title_object != null){
temp_card_obj.title = title_object.name;
}
let description_object = findNestedObj(jsonLd, "description");
if(description_object != null){
temp_card_obj.content = description_object.description;
}
let article_body_object = findNestedObj(jsonLd, "articleBody");
if(description_object != null && article_body_object != null) {
temp_card_obj.content = article_body_object.articleBody;
}
// items can be nested or not! the template uses the nested items
// finds objects inside a specific key/value object
let breadcrumbList_object = findNestedObjWithValue(jsonLd,"@type","BreadcrumbList");
if(breadcrumbList_object != null){
temp_card_obj.breadcrumbList = breadcrumbList_object;
}
let action_object = findNestedObj(jsonLd,"potentialAction");
if(action_object!= null){
temp_card_obj.potentialAction = action_object.potentialAction;
}
// render the template with data
return mustache.render(template, temp_card_obj);
}
jsonld2html.render = function render(jsonLd) {
return renderFromTemplate(jsonLd, getTemplate(findValueFromKey(jsonLd,"@type")));
}
jsonld2html.renderFromTemplate = renderFromTemplate;
export default jsonld2html