Skip to content

Commit

Permalink
AKA derived from methods
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Jun 14, 2024
1 parent 29e08be commit 036d939
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/plugins/category-listing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ module.exports = async function myPlugin(context, options) {
const tagNames = doc.tags.map(t => t.label);
const mitigates = (doc.frontMatter.practice?.mitigates ?? []).map(t => t.tag)
const attendant = (doc.frontMatter.practice?.attendant ?? []).map(t => t.tag)
const practices = (doc.frontMatter.method?.practices ?? []).map(t => t.tag)

const allTags = [...tagNames, ...mitigates, ...attendant]
const allTags = [...tagNames, ...mitigates, ...attendant, ...practices]

//console.log(JSON.stringify(allTags));

Expand Down
63 changes: 61 additions & 2 deletions src/theme/PracticeIntro/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import styles from './styles.module.css'
import { useDocById } from '@docusaurus/theme-common/internal'
import { usePluginData } from '@docusaurus/useGlobalData'
import { useLocation } from '@docusaurus/router';

function formatReadableTag(page) {
Expand All @@ -17,13 +18,59 @@ const Risk = ({tag, reason}) => {
)
}

const Method = ({ article, permalink, reason }) => {
return (
<li><a href={permalink}>{article}</a>: {reason}</li>
)
}

export default ({details}) => {
const Aka = ({ name, from, link }) => {
return (
<li>{name}{ from ? (<span class={styles.methodRef}> (in <a href={link}>{from}</a>)</span>): null }</li>
)
}


function isMethod(article, title) {
const matching = (article.frontMatter?.method?.practices ?? []).find(m => m.tag == title);
if (matching) {
return {
article: article.title,
permalink: article.permalink,
reason: matching.reason,
use: matching.use ?? []
}
} else {
return undefined;
}
}

function sortAndUnique(l) {
const uniqueArray = l.filter(function(item, pos) {
return l.findIndex(e => e.article == item.article) == pos;
})

const sorted = uniqueArray.sort((a, b) => a.article.localeCompare(b.article))
return sorted
}

export default ({details}) => {
const title = details.title

const pl = useLocation().pathname
const stripped = pl.endsWith('/') ? pl+"index" : pl
const imageLink = "/img/generated/single/" + stripped + ".png"
const allTags = usePluginData('category-listing');

const myTag = allTags[title]

//console.log("Found " + JSON.stringify(myTag.map(i => i.title)))

const methods = sortAndUnique(myTag.map(article => isMethod(article, title)).filter(i => i != undefined))

const mainAka = details.practice.aka.map(i => { return { name: i} })
const moreAka = methods.flatMap(m => m.use.map(u => { return {name: u, from: m.article, link: m.permalink, bob: JSON.stringify(m) }}))
const sortedAka = [...mainAka, ...moreAka].sort((a, b) => a.name.localeCompare(b.name))

return <div className={styles.practiceIntro}>
<div className={styles.columns}>
Expand All @@ -37,7 +84,8 @@ export default ({details}) => {
<h3>Also Known As</h3>
<ul>
{
details.practice.aka.map(i => <li>{i}</li>)
//JSON.stringify(sortedAka)
sortedAka.map(m => <Aka name={m.name} from={m.from} link={m.link} /> )
}
</ul>
<h3>Related Practices</h3>
Expand All @@ -58,5 +106,16 @@ export default ({details}) => {
details.practice.attendant.map(i => <Risk tag={i.tag} reason={i.reason} />)
}
</ul>
{
methods.length > 0 ? (
<>
<h3>Used By</h3>
<ul>
{
methods.map(i => <Method key={i.permalink} article={i.article} permalink={i.permalink} reason={i.reason} />)
}
</ul>
</>) : null
}
</div>
}
5 changes: 5 additions & 0 deletions src/theme/PracticeIntro/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
flex-grow: .7;
flex-shrink: .5;
padding: 1rem;
}

.methodRef {
color: darkgray;
font-style: italic;
}
2 changes: 1 addition & 1 deletion src/theme/RiskIntro/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default ({ fm }) => {
const allTags = usePluginData('category-listing');
const myTag = allTags[title]

console.log("Found " + JSON.stringify(myTag.map(i => i.title)))
//console.log("Found " + JSON.stringify(myTag.map(i => i.title)))

const mitigating = sortAndUnique(myTag.map(article => isPracticeMitigating(article, title)).filter(i => i != undefined))
const attendant = sortAndUnique(myTag.map(article => isPracticeAttendant(article, title)).filter(i => i != undefined))
Expand Down

0 comments on commit 036d939

Please sign in to comment.