Skip to content

Commit

Permalink
refactor: split into two files
Browse files Browse the repository at this point in the history
  • Loading branch information
rwd committed Jun 10, 2024
1 parent bdb5c10 commit 1c20abb
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 51 deletions.
6 changes: 3 additions & 3 deletions packages/portal/src/components/DS4CH/DS4CHLandingHero.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- eslint-disable vue/no-v-html -->
<div
class="hero-content-text-block"
v-html="parseMarkdownHtml(`# ${headline}\n${text}`)"
v-html="parseMarkdown(`# ${headline}\n${text}`)"
/>
<!-- eslint-enable vue/no-v-html -->
<SmartLink
Expand Down Expand Up @@ -42,7 +42,7 @@

<script>
import SmartLink from '@/components/generic/SmartLink';
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
const CSS_VARS_PRESETS = {
small: { w: 576, h: 896, fit: 'fill', q: 100, f: 'right' },
Expand Down Expand Up @@ -107,7 +107,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/portal/src/components/content/ContentCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
import ClientOnly from 'vue-client-only';
import SmartLink from '../generic/SmartLink';
import langAttributeMixin from '@/mixins/langAttribute';
import { stripMarkdown } from '@/utils/markdown.js';
import stripMarkdown from '@/utils/markdown/strip.js';
import truncateMixin from '@/mixins/truncate';
import { langMapValueForLocale } from '@europeana/i18n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!-- eslint-disable vue/no-v-html -->
<div
class="primary-cta-rich-text text-left"
v-html="parseMarkdownHtml(text)"
v-html="parseMarkdown(text)"
/>
<!-- eslint-enable vue/no-v-html -->
<SmartLink
Expand All @@ -26,7 +26,7 @@

<script>
import SmartLink from '../generic/SmartLink';
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
export default {
name: 'ContentPrimaryCallToAction',
Expand Down Expand Up @@ -55,7 +55,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
6 changes: 3 additions & 3 deletions packages/portal/src/components/landing/LandingEmbed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div
v-if="text"
class="text mb-3"
v-html="parseMarkdownHtml(text)"
v-html="parseMarkdown(text)"
/>
<!-- eslint-enable vue/no-v-html -->
</b-col>
Expand All @@ -46,7 +46,7 @@
</template>

<script>
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
import EmbedHTML from '@/components/embed/EmbedHTML';
export default {
Expand Down Expand Up @@ -104,7 +104,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
6 changes: 3 additions & 3 deletions packages/portal/src/components/landing/LandingHero.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<header class="hero-content">
<!-- eslint-disable vue/no-v-html -->
<div
v-html="parseMarkdownHtml(`# ${headline}\n${text}`)"
v-html="parseMarkdown(`# ${headline}\n${text}`)"
/>
<!-- eslint-enable vue/no-v-html -->
<SmartLink
Expand Down Expand Up @@ -40,7 +40,7 @@
<script>
import ImageWithAttribution from '@/components/image/ImageWithAttribution';
import SmartLink from '@/components/generic/SmartLink';
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
export default {
name: 'LandingHero',
Expand Down Expand Up @@ -110,7 +110,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div
v-if="text"
class="text mb-3"
v-html="parseMarkdownHtml(text)"
v-html="parseMarkdown(text)"
/>
<!-- eslint-enable vue/no-v-html -->
</b-col>
Expand Down Expand Up @@ -66,7 +66,7 @@

<script>
import ImageOptimised from '@/components/image/ImageOptimised';
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
import swiperMixin from '@/mixins/swiper';
import { Grid, Keyboard, Lazy, Navigation, Pagination } from 'swiper';
Expand Down Expand Up @@ -170,7 +170,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
6 changes: 3 additions & 3 deletions packages/portal/src/components/landing/LandingImageCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<!-- eslint-disable vue/no-v-html -->
<div
class="text"
v-html="parseMarkdownHtml(card.text)"
v-html="parseMarkdown(card.text)"
/>
<!-- eslint-enable vue/no-v-html -->
<SmartLink
Expand All @@ -54,7 +54,7 @@

<script>
import parityMixin from '@/mixins/parity.js';
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
const SRCSET_PRESETS = {
small: { w: 512, h: 342, fit: 'fill' },
Expand Down Expand Up @@ -145,7 +145,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div
v-if="text"
class="mb-3"
v-html="parseMarkdownHtml(text)"
v-html="parseMarkdown(text)"
/>
<!-- eslint-enable vue/no-v-html -->
</b-col>
Expand All @@ -35,7 +35,7 @@
</template>

<script>
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
export default {
name: 'LandingImageCardGroup',
Expand Down Expand Up @@ -83,7 +83,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
6 changes: 3 additions & 3 deletions packages/portal/src/components/landing/LandingInfoCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
class="text"
:class="{ 'mb-3': card.link }"
data-qa="landing info card text"
v-html="parseMarkdownHtml(card.text)"
v-html="parseMarkdown(card.text)"
/>
<!-- eslint-enable vue/no-v-html -->
<SmartLink
Expand All @@ -55,7 +55,7 @@
</template>

<script>
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
export default {
name: 'LandingInfoCard',
Expand Down Expand Up @@ -83,7 +83,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div
v-if="text"
class="text mb-3"
v-html="parseMarkdownHtml(text)"
v-html="parseMarkdown(text)"
/>
<!-- eslint-enable vue/no-v-html -->
</b-col>
Expand Down Expand Up @@ -44,7 +44,7 @@
</template>

<script>
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
export default {
name: 'LandingInfoCardGroup',
Expand Down Expand Up @@ -105,7 +105,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
6 changes: 3 additions & 3 deletions packages/portal/src/components/landing/LandingSubSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div
v-if="text"
class="text mx-auto mb-3"
v-html="parseMarkdownHtml(text)"
v-html="parseMarkdown(text)"
/>
<!-- eslint-enable vue/no-v-html -->
</div>
Expand Down Expand Up @@ -54,7 +54,7 @@

<script>
import contentfulMixin from '@/mixins/contentful.js';
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
export default {
name: 'LandingSubSection',
Expand Down Expand Up @@ -108,7 +108,7 @@
},
methods: {
parseMarkdownHtml
parseMarkdown
}
};
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
>
<div
class="card-content"
v-html="parseMarkdownHtml(slide.text)"
v-html="parseMarkdown(slide.text)"
/>
</b-card>
<!-- eslint-enable vue/no-v-html -->
Expand All @@ -54,7 +54,7 @@
</template>

<script>
import { parseMarkdownHtml } from '@/utils/markdown.js';
import parseMarkdown from '@/utils/markdown/parse.js';
import ImageWithAttribution from '@/components/image/ImageWithAttribution';
import { FULL_VIEWPORT_PRESETS } from '@/utils/contentful/imageCropPresets';
Expand Down Expand Up @@ -87,7 +87,7 @@
},
methods: {
parseMarkdownHtml,
parseMarkdown,
appearDisappearSlideImage() {
this.$refs.slideCards.forEach((card, index) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/portal/src/utils/markdown/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { marked } from 'marked';

export default (text) => {
return text ? marked.parse(text) : text;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const htmlRemovalPatternsFromTags = (tags) => {
return [/\n$/].concat(tags.map((tag) => new RegExp(`</?${tag}.*?>`, 'gi')));
};

export const parseMarkdownHtml = (text) => {
return text ? marked.parse(text) : text;
};

/**
* Strip markdown from text.
* This method FIRST converts markdown to HTML, then removes HTML tags.
Expand All @@ -19,7 +15,7 @@ export const parseMarkdownHtml = (text) => {
* @param {string[]} tags the HTML tags to be removed.
* @return {String} text value with HTML breaks
*/
export const stripMarkdown = (text, tags = ['']) => {
export default (text, tags = ['']) => {
text = marked.parse(text); // Marked adds newlines to the end of the string, and wraps it in a <p> tag.
for (const pattern of htmlRemovalPatternsFromTags(tags)) {
text = text.replace(pattern, '');
Expand Down
13 changes: 13 additions & 0 deletions packages/portal/tests/unit/utils/markdown/parse.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import parseMarkdown from '@/utils/markdown/parse.js';

describe('utils/markdown/parse.js', () => {
describe('parseMarkdown', () => {
it('parses markdown into html', () => {
const markdown = '# Heading 1';

const html = parseMarkdown(markdown);

expect(html).toContain('<h1 id="heading-1">Heading 1</h1>');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { parseMarkdownHtml, stripMarkdown } from '@/utils/markdown.js';

describe('utils/markdown', () => {
describe('parseMarkdownHtml', () => {
it('parses markdown into html', () => {
const markdown = '# Heading 1';

const html = parseMarkdownHtml(markdown);

expect(html).toContain('<h1 id="heading-1">Heading 1</h1>');
});
});
import stripMarkdown from '@/utils/markdown/strip.js';

describe('utils/markdown/strip.js', () => {
describe('stripMarkdown', () => {
describe('when the text is plain', () => {
const textBefore = 'Contains only plain text.';
Expand Down

0 comments on commit 1c20abb

Please sign in to comment.