Skip to content

Commit

Permalink
pkp/pkp-lib#9840 Add mdx and stories for new component
Browse files Browse the repository at this point in the history
  • Loading branch information
defstat committed May 28, 2024
1 parent 1bf3132 commit 2575193
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 43 deletions.
19 changes: 19 additions & 0 deletions src/components/CodeHighlighter/CodeHighlighter.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Stories, Meta} from '@storybook/blocks';

import * as CodeHighlighterStories from './CodeHighlighter.stories.js';

<Meta of={CodeHighlighterStories} />

# CodeHighlighter

## Usage

Use this component to display a formatted segment of code

## Supported Languages

The supported languages include:

- XML: `language="xml"`

<Stories />
37 changes: 37 additions & 0 deletions src/components/CodeHighlighter/CodeHighlighter.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import CodeHighlighter from './CodeHighlighter.vue';

export default {
title: 'Components/CodeHighlighter',
component: CodeHighlighter,
render: (args) => ({
components: {CodeHighlighter},
setup() {
return {args};
},
template: '<code-highlighter :code=args.code :language=args.language />',
}),
};

const sampleXML = `<?xml version="1.0" encoding="UTF-8"?>
<article>
<title>Open Access in Research</title>
<author>
<name>Jane Doe</name>
<affiliation>University of Knowledge</affiliation>
</author>
<journal>
<name>Journal of Open Science</name>
<year>2023</year>
</journal>
<doi>10.1234/jos.2023.001</doi>
<license type="CC-BY">https://creativecommons.org/licenses/by/4.0/</license>
<abstract>
This paper explores the significance of open access in scientific research.
</abstract>`;

export const XML = {
args: {
code: sampleXML,
language: 'xml',
},
};
84 changes: 41 additions & 43 deletions src/components/CodeHighlighter/CodeHighlighter.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<template>
<highlightjs
:language="language"
:code="code"
/>
<highlightjs :language="language" :code="code" />
</template>

<script>
// import hljs from 'highlight.js/lib/core';
import hljs from 'highlight.js/lib/common';
import hljs from 'highlight.js/lib/core';
import hljsVuePlugin from '@highlightjs/vue-plugin';
// Importing the language modules
Expand All @@ -16,51 +12,53 @@ import xml from 'highlight.js/lib/languages/xml';
// A map to store language modules
const languageMap = {
xml,
// Add more language modules imported above
xml,
// Add more language modules imported above
};
const supportedLanguages = Object.keys(languageMap);
export default {
components: {
highlightjs: hljsVuePlugin.component
},
props: {
code: {
type: String,
required: true
},
components: {
highlightjs: hljsVuePlugin.component,
},
props: {
code: {
type: String,
required: true,
},
language: {
type: String,
required: true,
validator(value) {
if (!supportedLanguages.includes(value)) {
console.error(`Unsupported language: ${value}. Supported languages are: ${supportedLanguages.join(', ')}`);
return false;
}
language: {
type: String,
required: true,
validator(value) {
if (!supportedLanguages.includes(value)) {
console.error(
`Unsupported language: ${value}. Supported languages are: ${supportedLanguages.join(', ')}`,
);
return false;
}
return true;
}
}
},
created() {
this.loadLanguageModule(this.language);
},
methods: {
loadLanguageModule(language) {
const languageModule = languageMap[language];
if (languageModule) {
hljs.registerLanguage(language, languageModule);
} else {
console.error(`Language module for ${language} is not available.`);
}
}
}
return true;
},
},
},
created() {
this.loadLanguageModule(this.language);
},
methods: {
loadLanguageModule(language) {
const languageModule = languageMap[language];
if (languageModule) {
hljs.registerLanguage(language, languageModule);
} else {
console.error(`Language module for ${language} is not available.`);
}
},
},
};
</script>

<style>
@import 'highlight.js/styles/default.css';
</style>
@import 'highlight.js/styles/default.css';
</style>

0 comments on commit 2575193

Please sign in to comment.