-
Notifications
You must be signed in to change notification settings - Fork 38
Add code-preview layout and web component example page #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
921e5f6
add code-preview template and example page
juliawu 46204c6
add escapes
juliawu 0d92963
s/preview/output
juliawu 747e8f5
add title to iframe
juliawu 3eda802
remove un-needed if
juliawu b831aa2
Use jekyll comments instead
juliawu 97d21d6
use nick's all-charts styling
juliawu 9109365
Merge branch 'master' into code-preview
juliawu 5b54856
allow popups in iframe so links work
juliawu fb0f66f
Merge branch 'code-preview' of https://github.com/juliawu/datacommons…
juliawu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,5 +47,6 @@ exclude: | |
| - vendor/gems/ | ||
| - vendor/ruby/ | ||
| - STYLE_GUIDE.md | ||
| - "**/README.md" | ||
|
|
||
| include: ["CNAME"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| {% comment %} | ||
| Displays another page inside an iframe with source code on the left and the page on the right | ||
|
|
||
| For maintainers: Set the url to display using the iframe_content_url variable in the frontmatter. | ||
| {% endcomment %} | ||
|
|
||
| <!doctype html> | ||
|
|
||
| <html lang="en"> | ||
| <head> | ||
| <title>{{ page.title | escape }} - Docs - Data Commons</title> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, initial-scale=1, shrink-to-fit=no" | ||
| /> | ||
| <link rel="icon" href="/assets/images/favicon.png" type="image/png" /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@300;400;500;700" | ||
| rel="stylesheet" | ||
| /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@300;400;500;700" | ||
| rel="stylesheet" | ||
| /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Public+Sans:wght@600&family=Roboto&family=Material+Icons&display=swap" | ||
| rel="stylesheet" | ||
| /> | ||
| <link href="/assets/css/styles.css" rel="stylesheet" /> | ||
| <link href="/assets/css/code-preview.css" rel="stylesheet" /> | ||
| <!-- Syntax highlighting for code --> | ||
| <link | ||
| rel="stylesheet" | ||
| href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" | ||
| /> | ||
| <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script> | ||
| </head> | ||
| <body> | ||
| <div id="main"> | ||
| {% include header.html %} | ||
| <main id="{{ main_id }}" class="container-fluid"> | ||
| <div id="content"> | ||
| <div id="source-code"> | ||
| <h3>Source Code</h3> | ||
| <pre><code id="code-target" class="language-html">Loading source code...</code></pre> | ||
| </div> | ||
| <div id="preview"> | ||
| <h3>Output</h3> | ||
| <iframe | ||
| id="iframe" | ||
| title="{{ page.title | escape }} Rendered Output" | ||
| src="{{ page.iframe_content_url | escape}}" | ||
| sandbox="allow-scripts allow-popups" | ||
| ></iframe> | ||
| </div> | ||
| </div> | ||
| </main> | ||
| {% include footer.html %} | ||
| </div> | ||
| <script> | ||
| // Fetch the source code for the page and display it in the source code | ||
| // div. | ||
| const fileUrl = "{{ page.iframe_content_url | escape }}"; | ||
| fetch(fileUrl) | ||
| .then((response) => { | ||
| if (!response.ok) throw new Error("Network response was not ok"); | ||
| return response.text(); | ||
| }) | ||
| .then((data) => { | ||
| // Escape the HTML so it doesn't render as actual elements | ||
| const codeTarget = document.getElementById("code-target"); | ||
| codeTarget.textContent = data; | ||
| Prism.highlightElement(codeTarget); | ||
| }) | ||
| .catch((err) => { | ||
| document.getElementById("code-target").textContent = | ||
| "Error loading source: " + err.message; | ||
| }); | ||
| </script> | ||
| {% include scripts.html %} | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Web Components Examples | ||
|
|
||
| This directory contains the pages that render the side-by-side source code and rendered output for the Data Commons web components examples. | ||
|
|
||
| ## How the examples are rendered | ||
|
|
||
| The examples are rendered into the `code-preview` layout in `/_layouts/code-preview.html`. | ||
|
|
||
| A `<script>` tag in the layout pulls the the raw HTML source of the file specified in the `iframe_content_url` frontmatter property and displays it in a `<code>` block. It then applies syntax highlighting to the code via [Prism.js](https://prismjs.com/). | ||
|
|
||
| The right-side live-render is an iframe that loads the same file. | ||
|
|
||
| ## How to add a new example | ||
|
|
||
| 1. Add a new HTML file to the assets/examples/web-components/ directory containing the full HTML for the example. | ||
| 2. Create a new markdown file in this directory that uses the code-preview layout. | ||
| 3. Add frontmatter to the new file following this template: | ||
|
|
||
| ```yaml | ||
| --- | ||
| layout: code-preview | ||
| title: Web components example - [SOME DESCRIPTIVE NAME HERE] | ||
| published: true | ||
| nav_exclude: true # This keeps the page from showing up in the left navigation | ||
| iframe_content_url: /assets/examples/web-components/[FILENAME].html | ||
| --- | ||
| ``` | ||
|
|
||
| The `iframe_content_url` should point to the HTML file you created in step 1. | ||
|
|
||
| 4. Finally, you can link to the new example using the path `/api/web_components/examples/[FILENAME].html` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| layout: code-preview | ||
| title: All Charts | ||
| parent: All Charts - Web Components Example | ||
| published: true | ||
| nav_exclude: true | ||
| iframe_content_url: /assets/examples/web-components/all-charts.html | ||
| --- | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| --- | ||
|
|
||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * CSS for the code-preview layout (e.g., /api/web_components/examples/all-charts.html) | ||
| */ | ||
|
|
||
| html, | ||
| body { | ||
| height: 100vh; | ||
| } | ||
|
|
||
| #main { | ||
| display: flex; | ||
| flex-direction: column; | ||
| height: 100%; | ||
|
|
||
| > main { | ||
| flex-grow: 1; | ||
| display: flex; | ||
| flex-direction: column; | ||
| min-height: 0; | ||
| padding: 0; | ||
| } | ||
| } | ||
|
|
||
| header, | ||
| footer { | ||
| flex-shrink: 0; | ||
| } | ||
|
|
||
| #content { | ||
| flex-grow: 1; | ||
| margin: 0; | ||
| min-height: 0; | ||
| display: flex; | ||
| overflow: hidden; | ||
|
|
||
| /* On smaller screens, show source code and preview top to bottom */ | ||
| @media (max-width: 1024px) { | ||
| flex-direction: column; | ||
| } | ||
| } | ||
|
|
||
| #preview, | ||
| #source-code { | ||
| width: 50%; | ||
| height: 100%; | ||
| display: flex; | ||
| flex-direction: column; | ||
| overflow: hidden; | ||
|
|
||
| h3 { | ||
| margin: 0; | ||
| padding: 10px; | ||
| background: #fafcff; | ||
| border-top: 1px solid #dee2e6; | ||
| border-bottom: 1px solid #dee2e6; | ||
| flex-shrink: 0; | ||
| font-size: 1rem; | ||
| font-weight: 500; | ||
| } | ||
|
|
||
| /* On smaller screens, show source code and preview top to bottom */ | ||
| @media (max-width: 1024px) { | ||
| width: 100%; | ||
| height: 50%; | ||
| min-height: 0; | ||
| } | ||
| } | ||
|
|
||
| #preview { | ||
| iframe { | ||
| border: none; | ||
| width: 100%; | ||
| flex-grow: 1; | ||
| min-height: 0; | ||
| } | ||
| } | ||
|
|
||
| #source-code { | ||
| pre { | ||
| margin: 0; | ||
| flex-grow: 1; | ||
| overflow: auto; | ||
| min-height: 0; | ||
| padding: 10px; | ||
| } | ||
| } | ||
|
|
||
| #code-target { | ||
| /* Reset specific height/overflow as pre handles it now */ | ||
| height: auto; | ||
| overflow: visible; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.