Skip to content

Commit

Permalink
Allow passing a "Badge ID" in codelabs metadata which will be passed …
Browse files Browse the repository at this point in the history
…to the "devsite-badge-to-earn" element and sisplay the Badge that can get earned for this codelab.
  • Loading branch information
nicolasgarnier committed Apr 16, 2019
1 parent 7b2716a commit 836c84f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 24 deletions.
6 changes: 3 additions & 3 deletions claat/parser/gdoc/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ func metaTable(ds *docState) {
switch strings.ToLower(stringifyNode(tr.FirstChild, true)) {
case "id", "url":
ds.clab.ID = s
case "author":
ds.clab.Authors = s
case "authors":
case "author", "authors":
ds.clab.Authors = s
case "badge", "badge id":
ds.clab.BadgeID = s
case "summary":
ds.clab.Summary = s
case "category", "categories":
Expand Down
4 changes: 4 additions & 0 deletions claat/parser/md/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
// Metadata constants for the YAML header
const (
MetaAuthors = "authors"
MetaBadgeID = "badge id"
MetaSummary = "summary"
MetaID = "id"
MetaCategories = "categories"
Expand Down Expand Up @@ -356,6 +357,9 @@ func addMetadataToCodelab(m map[string]string, c *types.Codelab) error {
case MetaAuthors:
// Directly assign the summary to the codelab field.
c.Authors = v
case MetaBadgeID:
// Directly assign the codelab ID to the codelab field.
c.BadgeID = v
case MetaSummary:
// Directly assign the summary to the codelab field.
c.Summary = v
Expand Down
3 changes: 2 additions & 1 deletion claat/render/template-devsite.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
{{if eq $i 0}}
<google-codelab-about codelab-title="{{$.Meta.Title}}"
{{if $.Updated}}last-updated="{{$.Updated}}"{{end}}
{{if $.Meta.Authors}}authors="{{$.Meta.Authors}}"{{end}}>
{{if $.Meta.Authors}}authors="{{$.Meta.Authors}}"{{end}}
{{if $.Meta.BadgeID}}badge-id="{{$.Meta.BadgeID}}"{{end}}>
</google-codelab-about>
{{end}}
{{.Content | renderHTML $.Env}}
Expand Down
45 changes: 27 additions & 18 deletions claat/render/tmpldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,26 +481,35 @@ var tmpldata = map[string]*template{
0x6f,0x72,0x73,0x3d,0x22,0x7b,0x7b,0x24,0x2e,0x4d,
0x65,0x74,0x61,0x2e,0x41,0x75,0x74,0x68,0x6f,0x72,
0x73,0x7d,0x7d,0x22,0x7b,0x7b,0x65,0x6e,0x64,0x7d,
0x7d,0x3e,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x3c,0x2f,0x67,0x6f,0x6f,
0x67,0x6c,0x65,0x2d,0x63,0x6f,0x64,0x65,0x6c,0x61,
0x62,0x2d,0x61,0x62,0x6f,0x75,0x74,0x3e,0xa,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7b,
0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7b,0x7b,0x2e,
0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x20,0x7c,0x20,
0x72,0x65,0x6e,0x64,0x65,0x72,0x48,0x54,0x4d,0x4c,
0x20,0x24,0x2e,0x45,0x6e,0x76,0x7d,0x7d,0xa,0x20,
0x7d,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x7b,0x7b,0x69,0x66,
0x20,0x24,0x2e,0x4d,0x65,0x74,0x61,0x2e,0x42,0x61,
0x64,0x67,0x65,0x49,0x44,0x7d,0x7d,0x62,0x61,0x64,
0x67,0x65,0x2d,0x69,0x64,0x3d,0x22,0x7b,0x7b,0x24,
0x2e,0x4d,0x65,0x74,0x61,0x2e,0x42,0x61,0x64,0x67,
0x65,0x49,0x44,0x7d,0x7d,0x22,0x7b,0x7b,0x65,0x6e,
0x64,0x7d,0x7d,0x3e,0xa,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x2f,0x67,
0x6f,0x6f,0x67,0x6c,0x65,0x2d,0x63,0x6f,0x64,0x65,
0x6c,0x61,0x62,0x2d,0x73,0x74,0x65,0x70,0x3e,0xa,
0x20,0x20,0x20,0x20,0x20,0x20,0x7b,0x7b,0x65,0x6e,
0x64,0x7d,0x7d,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,
0xa,0x20,0x20,0x20,0x20,0x3c,0x2f,0x67,0x6f,0x6f,
0x67,0x6c,0x65,0x2d,0x63,0x6f,0x64,0x65,0x6c,0x61,
0x62,0x3e,0xa,0x20,0x20,0x3c,0x2f,0x62,0x6f,0x64,
0x79,0x3e,0xa,0x3c,0x2f,0x68,0x74,0x6d,0x6c,0x3e,
0xa,
0x6c,0x61,0x62,0x2d,0x61,0x62,0x6f,0x75,0x74,0x3e,
0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x7b,0x7b,0x65,0x6e,0x64,0x7d,0x7d,0xa,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7b,
0x7b,0x2e,0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x20,
0x7c,0x20,0x72,0x65,0x6e,0x64,0x65,0x72,0x48,0x54,
0x4d,0x4c,0x20,0x24,0x2e,0x45,0x6e,0x76,0x7d,0x7d,
0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,
0x2f,0x67,0x6f,0x6f,0x67,0x6c,0x65,0x2d,0x63,0x6f,
0x64,0x65,0x6c,0x61,0x62,0x2d,0x73,0x74,0x65,0x70,
0x3e,0xa,0x20,0x20,0x20,0x20,0x20,0x20,0x7b,0x7b,
0x65,0x6e,0x64,0x7d,0x7d,0x7b,0x7b,0x65,0x6e,0x64,
0x7d,0x7d,0xa,0x20,0x20,0x20,0x20,0x3c,0x2f,0x67,
0x6f,0x6f,0x67,0x6c,0x65,0x2d,0x63,0x6f,0x64,0x65,
0x6c,0x61,0x62,0x3e,0xa,0x20,0x20,0x3c,0x2f,0x62,
0x6f,0x64,0x79,0x3e,0xa,0x3c,0x2f,0x68,0x74,0x6d,
0x6c,0x3e,0xa,
},
},
"md": &template{
Expand Down
1 change: 1 addition & 0 deletions claat/types/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Meta struct {
Duration int `json:"duration"` // Codelab duration in minutes
Title string `json:"title"` // Codelab title
Authors string `json:"authors,omitempty"` // Arbitrary authorship text
BadgeID string `json:"badge_id,omitempty"` // ID of the BAdge to grant on codelab completion on devsite
Summary string `json:"summary"` // Short summary
Theme string `json:"theme"` // Usually first item of Categories
Status *LegacyStatus `json:"status"` // Draft, Published, Hidden, etc.
Expand Down
12 changes: 12 additions & 0 deletions codelab-elements/google-codelab-about/google_codelab_about.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const LAST_UPDATED_ATTR = 'last-updated';
/** @const {string} */
const AUTHORS_ATTR = 'authors';

/** @const {string} */
const BADGE_ID_ATTR = 'badge-id';

/** @const {string} */
const CODELAB_TITLE_ATTR = 'codelab-title';

Expand All @@ -52,6 +55,9 @@ class CodelabAbout extends HTMLElement {

/** @private {?string} */
this.lastUpdated_ = null;

/** @private {?string} */
this.badgeId_ = null;
}

/**
Expand Down Expand Up @@ -97,6 +103,11 @@ class CodelabAbout extends HTMLElement {
this.codelabTitle_ = this.getAttribute(CODELAB_TITLE_ATTR);
}
break;
case BADGE_ID_ATTR:
if (this.hasAttribute(BADGE_ID_ATTR)) {
this.badgeId_ = this.getAttribute(BADGE_ID_ATTR);
}
break;
}

this.setupDom_();
Expand Down Expand Up @@ -126,6 +137,7 @@ class CodelabAbout extends HTMLElement {
lastUpdated: CodelabAbout.formatDate_(this.lastUpdated_),
authors: this.authors_,
codelabTitle: this.codelabTitle_.split(':').join(':||').split('||'),
badgeId: this.badgeId_,
});

this.hasSetup_ = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ google-codelab-about .about-card h2 {
font-size: 28px;
}

google-codelab-about .about-card > *:not(:first-child):not(:last-child) {
google-codelab-about .about-card > *:not(:first-child) {
padding-top: 30px;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
{@param? codelabTitle: list<string>}
{@param? lastUpdated: string}
{@param? authors: string}
{@param? badgeId: string}

{if $codelabTitle}
<div class="codelab-title">
Expand Down Expand Up @@ -56,6 +57,8 @@
{/if}
</div>

<devsite-badge-to-earn></devsite-badge-to-earn>
{if $badgeId}
<devsite-badge-to-earn badge-id="$badgeId"></devsite-badge-to-earn>
{/if}
</div>
{/template}

0 comments on commit 836c84f

Please sign in to comment.