Skip to content

Commit

Permalink
feat(gh-pages): the ability to include markdown into demo pages (#368)
Browse files Browse the repository at this point in the history
Co-authored-by: Anna <[email protected]>
  • Loading branch information
ala-n and abarmina authored Aug 5, 2021
1 parent 84e4f58 commit 7fa42fc
Show file tree
Hide file tree
Showing 32 changed files with 165 additions and 36 deletions.
14 changes: 11 additions & 3 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const { isDev } = require('./pages/views/_data/env');
const htmlmin = require('html-minifier');

const { isDev } = require('./pages/views/_data/env');
const { markdown } = require('./pages/views/_data/markdown');
const { MDRenderer } = require('./pages/views/_data/md-render');

module.exports = (config) => {
config.addWatchTarget('src/**/*.md');

config.addPassthroughCopy({
'pages/static/assets': 'assets',
'pages/static/tools': '.',
});

config.setLibrary('md', markdown);

config.addNunjucksAsyncShortcode('mdRender', MDRenderer.render);

config.addFilter('sortByName', (values) => {
if (!values || !Array.isArray(values)) {
console.error(`Unexpected values in 'sortByName' filter: ${values}`);
Expand All @@ -26,15 +35,14 @@ module.exports = (config) => {

config.addTransform('htmlmin', function (content, outputPath) {
if (outputPath && outputPath.endsWith('.html') && !isDev) {
let minified = htmlmin.minify(content, {
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
});
return minified;
}
return content;
});
Expand Down
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module.exports = {
// runner: 'jest-electron/runner',
// testEnvironment: 'jest-electron/environment',
testEnvironment: 'jsdom',
preset: 'ts-jest',
roots: ['src/modules'],
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-sonarjs": "^0.9.1",
"foreach-cli": "^1.8.1",
"highlight.js": "^11.1.0",
"html-minifier": "^4.0.0",
"husky": "^7.0.1",
"intersection-observer": "^0.12.0",
Expand Down
5 changes: 5 additions & 0 deletions pages/src/common/markdown.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.markdown-container {
& ul {
padding-left: 1.5rem;
}
}
8 changes: 4 additions & 4 deletions pages/src/common/page.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
margin: 0;
padding: 0;
box-sizing: border-box;
}
*:focus {
outline: 1px dotted gray;

&:focus {
outline: 1px dotted gray;
}
}

.footer {
Expand All @@ -22,7 +23,6 @@

body {
display: flex;
//min-width: 650px;
min-height: 100vh;
color: rgba(22, 0, 27, 0.9);
flex-direction: column;
Expand Down
3 changes: 3 additions & 0 deletions pages/src/localdev.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@import (inline) "./libs/bootstrap.css";
@import "./common/markdown.less";
@import "./common/page.less";
@import "./header/header.less";

@import "../../src/modules/all.less";
@import "../../src/modules/draft/all.less";

@import (inline) "../../node_modules/highlight.js/styles/github.css";
16 changes: 16 additions & 0 deletions pages/views/_data/markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const hljs = require('highlight.js');
const markdownLib = require('markdown-it');

const markdown = markdownLib({
html: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch { }
}
return '';
}
});

module.exports = { markdown };
55 changes: 55 additions & 0 deletions pages/views/_data/md-render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require('path');
const fsAsync = require('fs').promises;
const { JSDOM } = require('jsdom');

const { markdown } = require('./markdown');

const fileCache = new Map();

const parseFile = async (filePath) => {
const absolutePath = path.resolve(__dirname, '../../../', filePath);

if (fileCache.has(absolutePath)) return fileCache.get(absolutePath);

const data = await fsAsync.readFile(absolutePath);
const content = data.toString();
const renderedContent = markdown.render(content);

fileCache.set(absolutePath, renderedContent);
return renderedContent;
};

// <p><a name></a></p>
const unwrapAnchor = (anchor) => anchor && anchor.matches(':only-child') ? anchor.parentElement : anchor;

class MDRenderer {
static wrapContent(content) {
return `<div class="markdown-container">${content}</div>`;
}

static async render(filePath, startSel, endSel) {
try {
const content = await parseFile(filePath);

if (!startSel) return MDRenderer.wrapContent(content);

const { window } = new JSDOM(content);
const startAnchor = window.document.querySelector(`a[name='${startSel}']`);
const endAnchor = window.document.querySelector(`a[name='${endSel}']`);

let node = unwrapAnchor(startAnchor);
let endNode = unwrapAnchor(endAnchor);
let partContent = '';

for (node = node.nextSibling; !!node && node !== endNode; node = node.nextSibling) {
partContent += node.outerHTML || node.textContent;
}

return MDRenderer.wrapContent(partContent);
} catch (e) {
return `Rendering error: ${e}`;
}
}
}

module.exports = { MDRenderer };
4 changes: 2 additions & 2 deletions pages/views/_includes/header-menu.njk
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{% macro menu(title, collection) %}
<esl-trigger class="hd-dropdown-trigger" track-hover target="::next">
<button class="btn btn-link hd-dropdown-btn hd-dropdown-arrow {{ 'text-info' if collection in page.url}}">
<button class="btn btn-link hd-dropdown-btn hd-dropdown-arrow {{ 'text-info' if collection in page.url }}">
{{ title }}
</button>
</esl-trigger>
<esl-panel class="hd-dropdown-content" default-params="{hideDelay: 250}" fallback-duration="500">
{% if collections[collection] %}
<ul class="hd-dropdown-list">
{% for item in collections[collection] | sortByName %}
{% set isActive = page.url == item.url %}
{% set isActive = page.url === item.url %}
<li class="nav-item hd-dropdown-item {{ 'active' if isActive }}" {% if isActive %}aria-current="page"{% endif %}>
<a class="hd-dropdown-link" href="{{ item.url | url }}">{{ item.data.name }}</a>
</li>
Expand Down
8 changes: 4 additions & 4 deletions pages/views/_includes/header.njk
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<nav class="navbar navbar-expand-lg navbar-dark bg-dark esl-demo-header">
<div class="container flex-column flex-lg-row align-items-stretch align-items-lg-center">
<a class="navbar-brand logo-light mr-5" href="{{ '/' | url }}">
{% include "img/logo.svg" %}
{% include 'pages/static/assets/logo.svg' %}
<span class="ml-2">Exadel Smart Library</span>
</a>
{% from 'header-menu.njk' import menu with context %}
<ul class="navbar-nav flex-grow-1 flex-sm-row">
<li class="nav-item hd-dropdown">
{{ menu ("Components", "basic") }}
{{ menu ('Components', 'basic') }}
</li>
<li class="nav-item hd-dropdown">
{{ menu ("Form Components", "forms") }}
{{ menu ('Form Components', 'forms') }}
</li>
{% if env.isDev %}
<li class="nav-item hd-dropdown">
{{ menu ("Drafts", "drafts") }}
{{ menu ('Drafts', 'drafts') }}
</li>
{% endif %}

Expand Down
7 changes: 0 additions & 7 deletions pages/views/_includes/img/logo.svg

This file was deleted.

4 changes: 2 additions & 2 deletions pages/views/_layouts/basic.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "./html5.njk" %}
{% extends './html5.njk' %}
{% set scrollbar = true %}

{% block head %}
Expand All @@ -16,7 +16,7 @@
</script>

<link rel="icon" type="image/png" href="{{ '/assets/logo-64x64.png' | url }}" />
<link type="text/css" rel="stylesheet" href="{{ '/bundles/localdev.css' | url }}" />
<link rel="stylesheet" type="text/css" href="{{ '/bundles/localdev.css' | url }}" />

{% block extra_head %}{% endblock %}
{% endblock %}
Expand Down
2 changes: 2 additions & 0 deletions pages/views/basic/esl-accordion.njk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ tags: basic
---
{% import 'lorem.njk' as lorem %}

{% mdRender 'src/modules/esl-trigger/README.md', 'intro' %}

<section class="row">
<style>
.card-header[active] {
Expand Down
2 changes: 2 additions & 0 deletions pages/views/basic/esl-alert.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ containerCls: container
tags: basic
---

{% mdRender 'src/modules/esl-alert/README.md', 'intro' %}

<section class="row">
<div class="col-12">
<h3 class="text-center">Triggers</h3>
Expand Down
2 changes: 2 additions & 0 deletions pages/views/basic/esl-image.njk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ tags: basic
---
{% set imageSrcBase = '/assets/' | url %}

{% mdRender 'src/modules/esl-image/README.md', 'intro' %}

<section class="row">
<div class="col-12">
<h3 class="text-center">Mods</h3>
Expand Down
7 changes: 6 additions & 1 deletion pages/views/basic/esl-media.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ name: Esl Media
tags: basic
---

<div class="content container">
{% mdRender 'src/modules/esl-media/README.md', 'intro' %}
</div>

<div class="description">
<p>ESL Media - is a esl container that gives you the same API for different providers under the hood
along with helpers like <em>lazy loading</em>, <em>activate query</em>, <em>picture cropping</em>.</p>
Expand Down Expand Up @@ -149,7 +153,8 @@ tags: basic
</div>
</section>

<section class="col-12" data-demo="7">
{# Hide it temporary before playground integration #}
<section class="col-12" data-demo="7" hidden>
<h3 class="text-center">Load condition</h3>
<p class="text-center">Load condition feature allows us to define using ESLQuery condition when the video
source should be loaded.</p>
Expand Down
4 changes: 4 additions & 0 deletions pages/views/basic/esl-scrollbar.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ tags: basic
---
{% import 'lorem.njk' as lorem %}

<div class="content container">
{% mdRender 'src/modules/esl-scrollbar/README.md', 'intro' %}
</div>

<section class="row">
<style>
.test-popup {
Expand Down
2 changes: 2 additions & 0 deletions pages/views/basic/esl-tab.njk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ tags: basic
---
{% import 'lorem.njk' as lorem %}

{% mdRender 'src/modules/esl-tab/README.md', 'intro' %}

<section class="row">
<style>
.accordion-trigger {
Expand Down
2 changes: 2 additions & 0 deletions pages/views/basic/esl-toggleable.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ containerCls: container
tags: basic
---

{% mdRender 'src/modules/esl-toggleable/README.md', 'intro' %}

<section class="row">
<div class="col-12">
<h4>Click event</h4>
Expand Down
2 changes: 2 additions & 0 deletions pages/views/forms/esl-select-list.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ containerCls: container
tags: forms
---

{% mdRender 'src/modules/esl-forms/esl-select-list/README.md', 'intro' %}

<section class="row">
<div class="col-12">
<style>
Expand Down
2 changes: 2 additions & 0 deletions pages/views/forms/esl-select.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ containerCls: container
tags: forms
---

{% mdRender 'src/modules/esl-forms/esl-select/README.md', 'intro' %}

<section class="row">
<div class="col-12">
<style>
Expand Down
2 changes: 1 addition & 1 deletion pages/views/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ui_playground: github.com/exadel-inc/ui-playground
---

<div class="d-flex flex-column flex-md-row align-items-center">
<div class="brand-section logo-primary mr-2">{% include "img/logo.svg" %}</div>
<div class="brand-section logo-primary mr-2">{% include 'pages/static/assets/logo.svg' %}</div>
<p class="m-0 lead description">
Exadel Smart Library (<strong>ESL</strong>) is a web components based library that gives you a set of <strong>lightweight</strong>
and <strong>flexible</strong> custom elements to easily create basic UX modules and make your websites super fast.
Expand Down
10 changes: 8 additions & 2 deletions src/modules/esl-alert/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# [ESL](../../../README.md) Alert
# [ESL](../../../) Alert

Version: *1.0.0*

Authors: *Julia Murashko*

<a name="intro"></a>

ESLAlert is a component to show small notifications on your pages. ESLAlert can have multiple instances on the page.

---

##Usage
<a name="usage"></a>

## Usage

First, use the common approach to register component: `ESLAlert.register()`.
Then you can attach `<esl-alert>` component to the expected place in the document or initialize it globally by using `ESLAlert.init` shortcut.
Expand All @@ -30,6 +34,8 @@ Use CustomEvent `details` to customize alert. Alert `details` accepts the follow

If one of `esl-alert`s catches the activation event it will prevent its propagation to parent elements.

<a name="example"></a>

## Example
```html
<body>
Expand Down
4 changes: 3 additions & 1 deletion src/modules/esl-forms/esl-select-list/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# [ESL](../../../../README.md) Select List
# [ESL](../../../../) Select List

Version: *1.0.0*

Authors: *Alexey Stsefanovich (ala'n)*

<a name="intro"></a>

ESLSelectList is a component to show selectable list of items. Decorates native HTMLSelectElement
ESLSelectList is HTML5 form compatible. Uses HTMLSelectElement as a data model.

Expand Down
Loading

0 comments on commit 7fa42fc

Please sign in to comment.