Skip to content

Commit

Permalink
Merge pull request #979 from Bedrock-OSS/licensing
Browse files Browse the repository at this point in the history
Integrate licensing
  • Loading branch information
QuazChick authored Feb 9, 2025
2 parents 0166396 + 3ddb14a commit 0de02c2
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 69 deletions.
16 changes: 13 additions & 3 deletions docs/.vitepress/theme/components/Article.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<script setup lang="ts">
import { computed } from "vue";
import { useMediaQuery } from "@vueuse/core";
import useData from "../composables/data";
import Tag from "./content/Tag.vue";
import Contributors from "./content/Contributors.vue";
import ExternalIcon from "./icons/ExternalIcon.vue";
import EditLink from "./content/EditLink.vue";
import Outline from "./content/Outline.vue";
import Spoiler from "./content/Spoiler.vue";
import License from "./content/License.vue";
import Button from "./content/Button.vue";
const { frontmatter } = useData();
const { frontmatter, page } = useData();
const isMobileOutline = useMediaQuery("(max-width: 1300px)");
const isLicensePage = computed(() => page.value.relativePath.startsWith("licenses/"));
</script>

<template>
Expand All @@ -22,6 +28,8 @@ const isMobileOutline = useMediaQuery("(max-width: 1300px)");
<Tag v-for="name in frontmatter.tags" :key="name" :name />
</div>

<Button v-if="isLicensePage" :link="frontmatter.source">View Source <ExternalIcon /></Button>

<div v-if="frontmatter.show_outline ?? true">
<Spoiler v-if="isMobileOutline" title="Contents" open>
<Outline />
Expand All @@ -34,10 +42,12 @@ const isMobileOutline = useMediaQuery("(max-width: 1300px)");

<ClientOnly>
<Suspense>
<Contributors v-if="frontmatter.show_contributors ?? true" />
<Contributors v-if="frontmatter.show_contributors ?? !isLicensePage" />
</Suspense>
</ClientOnly>

<EditLink v-if="frontmatter.show_edit_link ?? true" />
<EditLink v-if="frontmatter.show_edit_link ?? !isLicensePage" />

<License v-if="frontmatter.license" />
</article>
</template>
31 changes: 31 additions & 0 deletions docs/.vitepress/theme/components/content/License.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup lang="ts">
import { computed } from "vue";
import useData from "../../composables/data";
import NavLink from "../navigation/NavLink.vue";
import { data as licenses } from "../../data/licenses/licenses.data";
const { frontmatter } = useData();
const normalizedLicenses = computed(() => {
const { license } = frontmatter.value;
if (typeof license === "string") return { main: license };
return license;
});
const mainLicense = computed(() => licenses[normalizedLicenses.value.main]);
const codeLicense = computed(() => licenses[normalizedLicenses.value.code]);
</script>

<template>
<hr style="margin-block: 1em" />
<p>
Text and image content on this page is licensed under the
<NavLink :link="mainLicense.link">{{ mainLicense.title }}</NavLink>
</p>
<p v-if="codeLicense">
Code samples on this page are licensed under the
<NavLink :link="codeLicense.link">{{ codeLicense.title }}</NavLink>
</p>
</template>
2 changes: 1 addition & 1 deletion docs/.vitepress/theme/components/icons/ExternalIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://www.w3.org/2000/svg"
width="1.3em"
height="1.3em"
style="vertical-align: middle"
style="vertical-align: -0.2lh"
viewBox="0 0 20 20"
fill="currentColor"
>
Expand Down
51 changes: 51 additions & 0 deletions docs/.vitepress/theme/data/licenses/licenses.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { readdirSync, statSync, readFileSync } from "fs";
import { defineLoader } from "vitepress";
import matter from "gray-matter";
import { join } from "path";

import { License } from "../../types";
import config from "../config";

import validateLicense from "./validateLicense";

export interface Licenses {
[id: string]: License;
}

declare const data: Licenses;
export { data };

const licensesDirectory = join(config.srcDir, "licenses");

export default defineLoader({
watch: join(licensesDirectory, "*.md"),
load() {
const licenses: Licenses = {};

const entries = readdirSync(licensesDirectory);

for (const entry of entries) {
const joinedPath = join(licensesDirectory, entry);
const stats = statSync(joinedPath);

// Don't include non-markdown files, or the section index page
if (!stats.isFile() || !entry.endsWith(".md") || entry === "index.md") continue;

const licenseId = entry.replace(/\.md$/, "");

const licenseMarkdown = readFileSync(joinedPath, "utf-8");
const frontMatter = matter(licenseMarkdown);

validateLicense(licenseId, frontMatter);

const { data } = frontMatter;

licenses[licenseId] = {
link: "/licenses/" + licenseId,
title: data.title,
};
}

return licenses;
},
});
16 changes: 16 additions & 0 deletions docs/.vitepress/theme/data/licenses/validateLicense.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GrayMatterFile } from "gray-matter";

export default function validateLicense(id: string, { data }: GrayMatterFile<string>) {
const errors: string[] = [];

if (data.title === undefined) {
errors.push("A license title must be defined but none was found.");
}
if (data.source === undefined) {
errors.push("A license source link must be defined but none was found.");
}

if (errors.length > 0) {
throw new Error(`License "${id}" has invalid frontmatter:\n- ${errors.join("\n- ")}\n`);
}
}
5 changes: 5 additions & 0 deletions docs/.vitepress/theme/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface SidebarLink {
data: Record<string, any>;
}

export interface License {
title: string;
link: string;
}

export interface Tag {
color: AccentColor;
text?: string;
Expand Down
Loading

0 comments on commit 0de02c2

Please sign in to comment.